1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Discuss in my forum

How to pass arguments to a Bash-script

Commands, Syntax, and Examples

By

Businesswoman working on laptop in modern office
David Lees/Iconica/Getty Images
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 "stats.sh" 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 example, if the name of the file to be processed is "songlist", you would enter the following command line:

sh stats.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:

FILE1=$1
wc $FILE1

For readability we 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 "$@"
do
   wc $FILE1
done

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

sh stats.sh songlist1 songlist2 songlist3

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

sh stats.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 this 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 such that you can pass in these parameters when the script is called. For example, 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
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 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.

  1. About.com
  2. Technology
  3. Linux
  4. Linux HowTos
  5. Bash How-To's
  6. How to pass arguments to a Bash-script

©2014 About.com. All rights reserved.