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

Positional parameter(bash中的位置参数) src=

 

 

如$0 $1 $2 $@.. 这些叫做positional parameter

 

 

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:

 

Positional parameter(bash中的位置参数) src=

 

 

 

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 $@ contains the value of all positional parameters, excluding $0.

 

The variable $* is the same as $@,except when it is double-quoted. 在不加双引号的时候$*和$@是一样,

When enclosed in double quotes, $* expands to $1c$2c$3c...  如果加了双引号了,$*会被翻译成 $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 beginning and end of one word.   c是$IFS的第一个字母,bash的internal field separator变量(默认为<space><tab><newline>),内置字段分割变量。(所以c是空格)

 

 

If the value of $IFS is "_" (an underscore), "$@" 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 $@ can be modified for the current shell. Everything after set -- is registered as a positional parameter.

 

set -- one two "three four"

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

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

In bash, $@ 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) provides 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