How to Pass Arguments to a Bash Script

Commands, syntax, and examples

Bash logo

Clayton Kroh

Write a Bash script so that it receives arguments that are specified when the script is called from the command line. Use this method when a script has to perform a slightly different function depending on the values of the input parameters, also called arguments.

Example of Passing Arguments in a Bash Script

BASH For Loops
BASH For Loops.

If you developed a script called stats.sh that counts the words in a file, it's best to pass the file name as an argument so that the same script can be used for all the files that will be processed. For example, if the name of the file to be processed is song list, enter the following at the command line:

sh stats.sh songlist

Arguments are accessed inside a script using the variables $1, $2, $3, and so on. The variable $1 refers to the first argument, $2 to the second argument, and $3 to the third argument. This is illustrated in the following example:

FILE1=$1
wc $FILE1

For readability, assign a variable with a descriptive name to the value of the first argument ($1), and then call the word count utility (WC) on the variable $FILE1.

If you have a variable number of arguments, use the $@ variable, which is an array of all the input parameters. This procedure uses a for loop to iteratively process each one, as illustrated in the following example:

for FILE1 in "$@"
do
wc $FILE1
done

Here's an example of how to call this script with arguments from the command line:

sh stats.sh songlist1 songlist2 songlist3

If an argument includes spaces, enclose it with single quotes. For example:

sh stats.sh 'songlist 1' 'songlist 2' 'songlist 3'

Flags Method

Frequently a script is written so that arguments can be passed in any order using flags. With the flags method, some of the arguments can be made optional.

For example, write a script that retrieves information from a database based on specified parameters, such as username, date, and product, and generates a report in a specified format. The script needs to be written in such a way so that these parameters are passed when the script is called. It might look like this:

makereport -u jsmith -p notebooks -d 10-20-2011 -f pdf

Bash enables this functionality with the getopts function. For the above example, use getopts as follows:

while getopts u:d:p:f: option
do
case "${option}"
in
u) USER=${OPTARG};;
d) DATE=${OPTARG};;
p) PRODUCT=${OPTARG};;
f) FORMAT=${OPTARG};;
esac
done

This is a while loop that uses the getopts function and a so-called optstring — in this case u:d:p:f: — to iterate through the arguments. The while loop walks through the optstring, which contains the flags that are used to pass arguments, and assigns the argument value provided for that flag to the variable option. The CASE statement then assigns the value of the variable option to a global variable that is used after all the arguments have been read.

Meanings for Colons

The colons in the optstring mean that values are required for the corresponding flags. In the above example of u:d:p:f:, all flags are followed by a colon. This means all flags need a value. If, for example, the d and f flags were not expected to have a value, u:dp:f would be the optstring.

A colon at the beginning of the optstring (for example, :u:d:p:f:) has a completely different meaning. It handles flags that are not represented in the optstring. In that case, the value of the option variable is set to ? and the value of OPTARG is set to the unexpected flag. This behavior displays a suitable error message informing you of the mistake.

Arguments that are not preceded by a flag are ignored by getopts. If flags specified in the optstring are not provided when the script is called then nothing happens, unless you specially handle this case in your code. Any arguments not handled by getops can still be captured with the regular $1, $2, and $3 variables.