Passing parameters to a bash function

Tag: bash Author: string_369 Date: 2011-05-17

I am trying to search how to pass parameters in a bash function, but what comes up is always how to pass parameter from the command line.

I would like to pass parameters within my script. I tried:

myBackupFunction("..", "...", "xx")


function myBackupFunction($directory, $options, $rootPassword) {
     ...
}

But the syntax is not correct, how to pass parameter to my function? Thanks!

Best Answer

There are two typical ways of declaring a function. I prefer the second approach.

function function_name {
   command...
} 

or

function_name () {
   command...
} 

To call a function with arguments:

function_name $arg1 $arg2

The function refers to passed arguments by position (as if they were positional parameters), that is, $1, $2, and so forth.

Example:

function_name () {
   echo "Parameter #1 is $1"
}

Also, you need to call your function AFTER you have declared it.

#!/bin/sh

foo 1  # this will fail because foo has not been declared yet.

foo() {
    echo "Parameter #1 is $1"
}

foo 2 # this will work.

Output:

./myScript.sh: line 2: foo: command not found
Parameter #1 is 2

Reference: Advanced Bash-Scripting Guide.

comments:

Thank you, it works, now I'm going to learn more from that guide.
When I write function name(){} I get an error about the '('. But when I write name(){} it works. Any ideas?
You have forgotten the spaces, try function name() {}. Maybe with a 'enter' before {}
I didn't know you could call bash functions without ()!
Good answer. My 2 cents: in shell constructs that reside in a file that is sourced (dotted) when needed, I prefer to use the function keyword and the (). My goal (in a file, not command line) is to increase clarity, not reduce the number of characters typed, viz, function myBackupFunction() compound-statement.

Other Answer1

Miss out the parens and commas:

 myBackupFunction ".." "..." "xx"

and the function should look like this:

function myBackupFunction() {
   # here $1 is the first parameter, $2 the second etc.

Other Answer2

Knowledge of high level programming languages (C/C++/Java/PHP/Python/Perl ...) would suggest to the layman that bash functions should work like they do in those other languages. Instead, bash functions work like shell commands and expect arguments to be passed to them in the same way one might pass an option to a shell command (ls -l). In effect, function arguments in bash are treated as positional parameters ($1, $2..$9, ${10}, ${11}, and so on). This is no surprise considering how getopts works. Parentheses are not required to call a function in bash.


(Note: I happen to be working on Open Solaris at the moment.)

# bash style declaration for all you PHP/JavaScript junkies. :-)
# $1 is the directory to archive
# $2 is the name of the tar and zipped file when all is done.
function backupWebRoot ()
{
    tar -cvf - $1 | zip -n .jpg:.gif:.png $2 - 2>> $errorlog && echo -e "\nTarball 
        created!\n"
}


# sh style declaration for the purist in you. ;-)
# $1 is the directory to archive
# $2 is the name of the tar and zipped file when all is done.
backupWebRoot ()
{
    tar -cvf - $1 | zip -n .jpg:.gif:.png $2 - 2>> $errorlog && echo -e "\nTarball 
        created!\n"
}


#In the actual shell script
#$0               $1            $2

backupWebRoot ~/public/www/ webSite.tar.zip