Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Maybe I am looking at this the wrong way.. But here I what I am trying to do. Do most of my work with Java but just switched to a unix (bash) environment. I am doing this:

[~/Desktop/bashPlay]% cat myFunc

        echo "Hello World"

        echo "Testing"

echo "this is a test"   
ls2 # this calls a function

[~/Desktop/bashPlay]% myFunc
this is a test
Hello World

But I have two functions in my file and I want to be able to call them separately from the command line. Ie: myFunc.ls2() or even just ls2. I know that I can add the functions to my .bashrc file but is there any other way that I can execute these functions without adding them to my .bashrc?

share|improve this question
up vote 15 down vote accepted

One way to do this, that involves a bit more typing, is via the source command. To call a function from myFunc you could use source myFunc; ls2 and it would output Hello World.

So for example, I have a file called


function talk()
        echo "hi!"

now I want to call it's talk() function from the command line:

[john@awesome ~]$ source; talk

to call it from another bash script:


You can also put each in a separate script and add them in a directory which is in your PATH variable.

so for example, in one script called hello you'd have:

echo "Hello World"

now put it in one of the directories in your PATH, which you can view by running echo $PATH. You can add another directory to your PATH if you'd like or use an existing one. Once you've copied the file there, make it executable with chmod +x filename.

share|improve this answer
+1 makes sense. – sixtyfootersdude Feb 8 '10 at 15:41
Just to be clear: you only have to source the file once per script or shell startup. You can call the function as many times as you want after that without referencing the sourced file again. – Dennis Williamson Feb 8 '10 at 18:33
Thanks a ton :) – Sainath S.R May 10 at 11:35

Another approach would be to create a script called ( in the ~/bin directory for example) .

In this script, you add all your personal function definitions (let's say every time you add a function you add it to this file...)

Finally you just have to add the source ~/bin/ line to your .bashrc file. This way you will be able to call them from the command line, your .bashrc will stay clean, and you will have a specific place for your personal functions.

share|improve this answer

If you are like me, you dont want to clutter your environment with functions. You also have a group of functions that belong together in terms of what they do, so putting them in the same script file makes sense. (I know that a folder with multiple files could serve the same purpose). Here is a possible solution that allows you to call a specific function in the script:

$ cat    

ls2() {
        echo "Hello World"

ls3() {
        echo "Testing $*"

# the next line calls the function passed as the first parameter to the script.
# the remaining script arguments can be passed to this function.

$1 $2 $3 $4 $5 

$ ./ ls2    
Hello World   
$ ./ ls3    
$ ./ ls3 first_arg    
Testing first_arg    
share|improve this answer

The dot operator or source builtin in bash is analogous to the import statement in Java.

You can read more about the dot operator or the source builtin.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .