How to Pass Arguments to a Bash Script

Use one of two methods to transmit arguments to a script at run time

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.

Computer monitor with bash scripting illustrated

Lifewire / Ran Zheng

Example of Passing Arguments in a Bash Script

If you developed a script called 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 songlist, enter the following at the command line:

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. For example, in the script:

wc $FILE1

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 or whatever else you may want to do.

If you require 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 "$@"
wc $FILE1

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

sh songlist1 songlist2 songlist3

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

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
case "${option}"

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.

The starting colon puts getopts into 'silent reporting mode'. Instead of totally erroring the script, any unexpected argument fills the ? option and you can look for or handle that as necessary. Without the initial colon in the optstring, the terminal displays an error. For example, I have the script from above that counts words. It's expecting a syntax like " -f filename". Without the colon, if I try " -h", I get: -h illegal option -- h

Whereas, with the colon, I can then put a line in my script expecting something like this, such as - ?) echo "No -${OPTARG} argument found.";; . This then allows me to capture and handle the unexpected entry (in this case by outputting my own error message). -h
No -h argument found.

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.

Was this page helpful?