How to Pass Arguments to a Bash-Script

Commands, syntax and examples

Bash logo

You can write a bash script such that it receives arguments specified when the script is called from the command line. This method is used when a script has to perform a slightly different function depending on the values of input parameters (the arguments).

For example, you may have a script called "" that performs a particular operation on a file, such as counting its words. If you want to be able to use that script on many files, it is best to pass the file name as an argument, so that you can use the same script for all the files to be processed. For instance, if the name of the file to be processed is "songlist", you would enter the following command line:

sh songlist

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

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 this variable ($FILE1).

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

for FILE1 in "$@"
wc $FILE1

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

sh songlist1 songlist2 songlist3

If an argument has spaces, you need to enclose it with single quotes. For example:

sh 'songlist 1' 'songlist 2' 'songlist 3'

Frequently a script is written such that the user can pass in arguments in any order using flags. With the flags method, you can also make some of the arguments optional.

Let say you have 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". Now you want to write your script so that you can pass in these parameters 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, you could 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 can be 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 can used after all the arguments have been read.

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

A colon at the beginning of the optstring, for example ":u:d:p:f:", has a completely different meaning. It allows you to handle 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. The allows you to display a suitable error message informing the user 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, 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, etc. variables.