The Computer Oracle

How to call bash functions

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Underwater World

--

Chapters
00:00 How To Call Bash Functions
00:34 Accepted Answer Score 46
01:33 Answer 2 Score 2
01:48 Answer 3 Score 8
02:16 Answer 4 Score 16
02:49 Thank you

--

Full question
https://superuser.com/questions/106272/h...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#bash

#avk47



ACCEPTED ANSWER

Score 46


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 say.sh:

#!/bin/bash

function talk()
{
        echo "hi!"
}

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

[john@awesome ~]$ source say.sh; talk
hi!

to call it from another bash script:

#!/bin/bash
source say.sh
talk

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:

#!/bin/bash
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.




ANSWER 2

Score 16


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 functions.sh    
#!/bin/bash

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 

$ ./functions.sh ls2    
Hello World   
$ ./functions.sh ls3    
Testing     
$ ./functions.sh ls3 first_arg    
Testing first_arg    
$



ANSWER 3

Score 8


Another approach would be to create a script called functions.sh ( 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/functions.sh 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.




ANSWER 4

Score 2


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.