Linux has a rich and powerful set of ways to supply parameters to bash scripts.
In this tutorial, we’ll look at a few ways of doing this.
2. Argument List
Arguments can be passed to a bash script during the time of its execution, as a list, separated by space following the script filename. This comes in handy when a script has to perform different functions depending on the values of the input.
For instance, let’s pass a couple of parameters to our script start.sh:
sh start.sh development 100
2.1. Using Single Quote
If the input list has arguments that comprise multiple words separated by spaces, they need to be enclosed in single quotes.
For instance, in the above-mentioned example, if the first argument to be passed is development mode instead of development, it should be enclosed in single quotes and passed as ‘development mode’:
sh start.sh 'development mode' 100
2.2. Using Double Quotes
Arguments that require evaluation must be enclosed in double-quotes before passing them as input.
Consider a bash script copyFile.sh that takes in two arguments: A file name and the directory to copy it to:
sh copyFile.sh abc.txt "$HOME"
Here, the $HOME variable gets evaluated to the user’s home directory, and the evaluated result is passed to the script.
2.3. Escaping Special Characters
If the arguments that need to be passed have special characters, they need to be escaped with backslashes:
sh printStrings.sh abc a@1 cd\$ 1\*2
The characters $ and * do not belong to the safe set and hence are escaped with backslashes.
These rules for using single quotes, double quotes, and escape characters remain the same for the subsequent sections as well.
Arguments can also be passed to the bash script with the help of flags. These flags are usually single character letters preceded by a hyphen. The corresponding input value to the flag is specified next to it separated by space.
Let’s consider the following example of a user registration script, userReg.sh which takes 3 arguments: username, full name, and age:
sh userReg.sh -u abc -f Abc -a 25
Here, the input to the script is specified using the flags (u, f and a) and the script processes this input by fetching the corresponding values based on the flag.
4. Environment Variables
Bash scripts can also be passed with the arguments in the form of environment variables. This can be done in either of the following ways:
- Specifying the variable value before the script execution command
- Exporting the variable and then executing the script
Let’s look at the following example of a script processor.sh, which takes two variables var1 and var2 as input.
As mentioned above these variables can be fed as input to the script:
var1=abc var2=c\#1 sh processor.sh
Here, we’re first specifying the values of the var1 and var2 variables before invoking the script execution, in the same command.
The same can also be achieved by exporting var1 and var2 as an environment variable and then invoking the script execution:
export var1=abc export var2=c\#1 sh processor.sh
5. Last Argument Operator (!$)
The last argument of the previous command, referred by !$ can also be passed as an argument to a bash script.
Let’s suppose we’re again copying files, and the destination is the user home directory. Using the last argument operator, we can pass the input to the script:
cd $HOME sh copyFile.sh abc.txt !$
In the first command, we navigate to the user home and when the second command is invoked, !$ gets evaluated to the last argument of the previous command which is $HOME and hence the resultant command gets evaluated to:
sh copyFile.sh abc.txt $HOME
6. Pipe Operator (|)
Pipe operator (|) in combination with the xargs command can be used to feed input to the bash scripts.
Let’s revisit the earlier example of printStrings.sh script which takes the input as a list of strings and prints them.
Instead of passing these strings as arguments in the command line, they can be added inside a file. Then, this file, with the help of the pipe operator and xargs command, can be used as the input:
cat abc.txt | xargs printStrings.sh
Here, the cat command outputs the strings that have been added in the file. These are then passed to the xargs command through the pipe operator, which collects this list and passes it to the script.
In conclusion, we have seen different ways of passing command-line arguments to the bash script during run time and how different types of input like multi-word arguments, arguments with special characters need to be handled.