Positional parameter

Updated: 11/10/2017 by Computer Hope
Positional parameters

A positional parameter is an argument specified on the command line, used to launch the current process in a shell. Positional parameter values are stored in a special set of variables maintained by the shell.

Variables which store positional parameters

A typical example of a shell that uses positional parameters is bash. You can use bash on Linux, BSD, macOS X, and Windows 10.

Consider the following bash command. The command name is mycommand. The command line has three parameters: one, two, and three four.

mycommand one two "three four"
Tip

Positional parameters are delimited by a space. The shell interprets the things after the spaces as individual parameters. If the parameter itself contains a space, enclose it in quotation marks, as in "three four," above.

While mycommand is running, bash provides it with the following shell variables:

Variable name Value
$0 mycommand
$1 one
$2 two
$3 three four
$# 3
[email protected] one two three four
$* one two three four

The variable $0 is set to the first word of the command — the command name. This variable is useful because commands can be renamed, or executed using a symbolic link. Some programs behave differently depending on the command name used to run the program. $0 allows a program to see what command name launched it.

The variables $1, $2, and $3 contain the values of the first, second, and third parameters, respectively. If there was a fourth parameter, its value would be placed in the variable $4. Parameters greater than 9 can be accessed by using curly braces around the number; for instance, ${10} would be the tenth parameter, and ${123} would be the 123rd.

The variable $# contains the number of positional parameters, excluding $0.

The variable [email protected] contains the value of all positional parameters, excluding $0.

The variable $* is the same as [email protected], except when it is double-quoted. When enclosed in double quotes, $* expands to $1c$2c$3c... where c is the first character of $IFS, bash's internal field separator variable. The IFS is used for word splitting, and its default value is "space, tab, or newline" — this is where bash sees the end of one word and the beginning of the next.

If the value of $IFS is "_" (an underscore), "[email protected]" expands to:

 one two three four

Whereas "$*" expands to:

 one_two_three four

Examples

Here are some examples of using positional parameters in bash.

Using the set built-in command, the value of [email protected] can be modified for the current shell. Everything after set -- will be registered as a positional parameter.

set -- one two "three four"

Now we can iterate over these variables using for ... in:

for arg in [email protected]; do echo "$arg"; done
one
two
three four

In bash, [email protected] is the default list to iterate when you run for, so this command also works:

for arg; do echo "$arg"; done
one
two
three four

Iterating over "$*" (with double quotes) will provide a concatenated string of positional parameters, joined with the first character of $IFS:

IFS=","; for arg in "$*"; do echo "$arg"; done
one,two,three four

For more information, see the documentation of the bash command shell.

Parameter, Programming terms