Bash Scripting – Passing parameters to a script
Other Learning Articles that you may like to read
Free Courses We Offer
Paid Training Courses we Offer
The most basic method of passing data to your shell script is to use command line parameters. Command line parameters allow you to add data values to the command line when you execute the script:
$ ./addem 10 30
This example passes two command line parameters (10 and 30) to the script addem. The script handles the command line parameters using special variables. The following sections describe how to use command line parameters in your bash shell scripts.
Reading parameters
The bash shell assigns special variables, called positional parameters, to all of the command line parameters entered. This includes the name of the script the shell is executing. The positional parameter variables are standard numbers, with $0 being the script’s name, $1 being the first parameter, $2 being the second parameter, and so on, up to $9 for the ninth parameter.
Here’s a simple example of using one command line parameter in a shell script:
$ cat test1.sh
#!/bin/bash
# using one command line parameter
#
factorial=1
for (( number = 1; number <= $1 ; number++ ))
do
factorial=$[ $factorial * $number ]
done
echo The factorial of $1 is $factorial
$
$ ./test1.sh 5
The factorial of 5 is 120
$
You can use the $1 variable just like any other variable in the shell script. The shell script automatically assigns the value from the command line parameter to the variable; you don’t need to do anything with it.
If you need to enter more command line parameters, each parameter must be separated by a space on the command line:
$ cat test2.sh
#!/bin/bash
# testing two command line parameters
#
total=$[ $1 * $2 ]
echo The first parameter is $1.
echo The second parameter is $2.
echo The total value is $total.
$
$ ./test2.sh 2 5
The first parameter is 2.
The second parameter is 5.
The total value is 10.
$
The shell assigns each parameter to the appropriate variable.
In the preceding example, the command line parameters used were both numerical values. You can also use text strings in the command line:
$ cat test3.sh
#!/bin/bash
# testing string parameters
#
echo Hello $1, glad to meet you.
$
$ ./test3.sh Rich
Hello Rich, glad to meet you.
$
The shell passes the string value entered into the command line to the script. However, you’ll have a problem if you try to do this with a text string that contains spaces:
$ ./test3.sh Rich Blum
Hello Rich, glad to meet you.
$
Remember that each of the parameters is separated by a space, so the shell interpreted the space as just separating the two values. To include a space as a parameter value, you must use quotation marks (either single or double quotation marks):
$ ./test3.sh ‘Rich Blum’
Hello Rich Blum, glad to meet you.
$
$ ./test3.sh “Rich Blum”
Hello Rich Blum, glad to meet you.
$
NOTE
The quotation marks used when you pass text strings as parameters are not part of the data. They just delineate the beginning and the end of the data.
If your script needs more than nine command line parameters, you can continue, but the variable names change slightly. After the ninth variable, you must use braces around the variable number, such as ${10}. Here’s an example of doing that:
$ cat test4.sh
#!/bin/bash
# handling lots of parameters
#
total=$[ ${10} * ${11} ]
echo The tenth parameter is ${10}
echo The eleventh parameter is ${11}
echo The total is $total
$
$ ./test4.sh 1 2 3 4 5 6 7 8 9 10 11 12
The tenth parameter is 10
The eleventh parameter is 11
The total is 110
$
This technique allows you to add as many command line parameters to your scripts as you could possibly need.
Reading the script name
You can use the $0 parameter to determine the script name the shell started from the command line. This can come in handy if you’re writing a utility that can have multiple functions.
$ cat test5.sh
#!/bin/bash
# Testing the $0 parameter
#
echo The zero parameter is set to: $0
#
$
$ bash test5.sh
The zero parameter is set to: test5.sh
$
However, there is a potential problem. When using a different command to run the shell script, the command becomes entangled with the script name in the $0 parameter:
$ ./test5.sh
The zero parameter is set to: ./test5.sh
$
There is another potential problem. When the actual string passed is the full script path, and not just the script’s name, the $0 variable gets set to the full script path and name:
$ bash /home/Christine/test5.sh
The zero parameter is set to: /home/Christine/test5.sh
$
If you want to write a script that performs different functions based on just the script’s name, you’ll have to do a little work. You need to be able to strip off whatever path is used to run the script. Also, you need to be able to remove any entangled commands from the script.
Fortunately, there’s a handy little command available that does just that. The basename command returns just the script’s name without the path:
$ cat test5b.sh
#!/bin/bash
# Using basename with the $0 parameter
#
name=$(basename $0)
echo
echo The script name is: $name
#
$ bash /home/Christine/test5b.sh
The script name is: test5b.sh
$
$ ./test5b.sh
The script name is: test5b.sh
$
Now that’s much better. You can use this technique to write scripts that perform different functions based on the script name used. Here’s a simple example:
$ cat test6.sh
#!/bin/bash
# Testing a Multi-function script
#
name=$(basename $0)
#
if [ $name = “addem” ]
then
total=$[ $1 + $2 ]
#
elif [ $name = “multem” ]
then
total=$[ $1 * $2 ]
fi
#
echo
echo The calculated value is $total
#
$
$ cp test6.sh addem
$ chmod u+x addem
$
$ ln -s test6.sh multem
$
$ ls -l *em
-rwxrw-r--. 1 Christine Christine 224 Jun 30 23:50 addem
lrwxrwxrwx. 1 Christine Christine 8 Jun 30 23:50 multem -> test6.sh
$
$ ./addem 2 5
The calculated value is 7
$
$ ./multem 2 5
The calculated value is 10
$
The example creates two separate filenames from the test6.sh script, one by just copying the file to a new script (addem) and the other by using a symbolic link (see Chapter 3) to create the new script (multem). In both cases, the script determines the script’s base name and performs the appropriate function based on that value.
Testing parameters
Be careful when using command line parameters in your shell scripts. If the script is run without the parameters, bad things can happen:
$ ./addem 2
./addem: line 8: 2 + : syntax error: operand expected (error
token is “ ”)
The calculated value is
$
When the script assumes there is data in a parameter variable, and no data is present, most likely you’ll get an error message from your script. This is a poor way to write scripts. Always check your parameters to make sure the data is there before using it:
$ cat test7.sh
#!/bin/bash
# testing parameters before use
#
if [ -n “$1” ]
then
echo Hello $1, glad to meet you.
else
echo “Sorry, you did not identify yourself. ”
fi
$
$ ./test7.sh Rich
Hello Rich, glad to meet you.
$
$ ./test7.sh
Sorry, you did not identify yourself.
$
In this example, the -n test evaluation was used to check for data in the $1 command line parameter. In the next section, you’ll learn another way to check command line parameters.