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 can I pass a parameter to my function?
To pass any number of arguments to the bash function simply put them right after the function's name, separated by a space.
Arguments can be passed to the script when it is executed, by writing them as a space-delimited list following the script file name. Inside the script, the $1 variable references the first argument in the command line, $2 the second argument and so forth. The variable $0 references to the current script.
$2 is the second command-line argument passed to the shell script or function. Also, know as Positional parameters.
Knowledge of high level programming languages (C/C++, Java, PHP, Python, Perl, etc.) would suggest to the layman that Bourne Again Shell (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 (e.g. 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. Do not use parentheses to call a function in Bash.
(Note: I happen to be working on OpenSolaris 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
Want to use names for variables? Just do something this.
local filename=$1 # The keyword declare can be used, but local is semantically more specific.
Be careful, though. If an argument to a function has a space in it, you may want to do this instead! Otherwise, $1
might not be what you think it is.
local filename="$1" # Just to be on the safe side. Although, if $1 was an integer, then what? Is that even possible? Humm.
Want to pass an array to a function by value?
callingSomeFunction "${someArray[@]}" # Expands to all array elements.
Inside the function, handle the arguments like this.
function callingSomeFunction () { for value in "$@" # You want to use "$@" here, not "$*" !!!!! do : done }
Need to pass a value and an array, but still use "$@" inside the function?
function linearSearch () { local myVar="$1" shift 1 # Removes $1 from the parameter list for value in "$@" # Represents the remaining parameters. do if [[ $value == $myVar ]] then echo -e "Found it!\t... after a while." return 0 fi done return 1 } linearSearch $someStringValue "${someArray[@]}"
In Bash 4.3 and above, you can pass an array to a function by reference by defining the parameter of a function with the -n
option.
function callingSomeFunction () { local -n someArray=$1 # also ${1:?} to make the parameter mandatory. for value in "${someArray[@]}" # Nice! do : done } callingSomeFunction myArray # No $ in front of the argument. You pass by name, not expansion / value.
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 their position (not by name), that is $1
, $2
, and so forth. $0
is the name of the script itself.
Example:
function_name () { echo "Parameter #1 is $1" }
Also, you need to call your function after it is declared.
#!/usr/bin/env 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With