In certain situations, we may need to pass arguments or parameters to functions. In such situations, we can pass arguments as follows.
Calling the script with command-line parameters is as follows:
$ name arg1 arg2 arg3 . . .
Let's type a function as follows:
$ hello() { echo "Hello $1, let us be a friend."; }
Call the function in the command line as follows:
$ hello Ganesh
Output:
Hello Ganesh, let us be a friend
Let's write the script function_07.sh. In this script, we pass command-line parameters to the script as well as the function:
#!/bin/bash
quit()
{
exit
}
ex()
{
echo $1 $2 $3
}
ex Hello hi bye# Function ex with three arguments
ex World# Function ex with one argument
echo $1# First argument passed to script
echo $2# Second argument passed to script
echo $3# Third argument passed to script
quit
echo fooTest the script as follows:
$ chmod +x function_07.sh $ ./function_07.sh One Two Three
Output:
Hello hi bye World One Two Three
We can observe from the output that the parameters passed to the function are local to the function. In global scope, the command-line parameters to script are available as $1, $2, $3, and more.
Another example script called function_08.sh to pass multiple arguments to the function is as follows:
#!/bin/bash
countries()
{
# let us store first argument $1 in variable temp
temp=$1
echo "countries(): \$0 = $0" # print command
echo "countries(): \$1 = $1" # print first argument
echo "countries(): total number of args passed = $#"
echo "countries(): all arguments (\$*) passed = -\"$*\""
}
# Call function with one argument
echo "Calling countries() for first time"
countriesUSA
# Call function with three arguments
echo "Calling countries() second time "
countriesUSA India JapanTest the script as follows:
$ chmod +x function_08.sh $ ./function_08.sh
Output:
Calling countries() for first time countries(): $0 = ./hello.sh countries(): $1 = USA countries(): total number of args passed = 1 countries(): all arguments ($*) passed = -"USA" Calling countries() second time countries(): $0 = ./hello.sh countries(): $1 = USA countries(): total number of args passed = 3 countries(): all arguments ($*) passed = -"USA India Japan"
We can create a function that could create a new directory and change to it during the execution of the program. The script function_09.sh is as follows:
#!/bin/bash
# mcd: mkdir + cd; creates a new directory and
# changes into that new directory
mcd ()
{
mkdir $1
cd $1
}
mcd test1The preceding script will create the test1 folder in the current folder and change the path to the test1 folder.
A common task in many scripts is to ask users to input an answer as either Yes or No. In such situations, the following script function_10.sh would be very useful:
#!/bin/bash
yesno ( )
{
while true .
do
echo "$*"
echo "Please answer by entering yes or no : "
read reply
case $reply in
yes)
echo "You answered Yes"
return 0
;;
no)
echo "You answered No"
return 1
;;
* )
echo "Invalid input"
;;
esac
done
}
Yesno$ chmod +x function_10.sh $ ./function_10.sh
Output:
Please answer by entering yes or no: yes "You answered Yes" $ ./function_10.sh Please answer by entering yes or no: no "You answered No"