On the meaning of shell variables $ා, [email protected], $0, $1, $2 in Linux

Time:2020-7-27

Excerpt from: ABS_ GUIDE

Download address: http://www.tldp.org/LDP/abs/abs-guide.pdf

The meaning of shell variables $#, [email protected], $0, $1, $2 in Linux

Variable description:

$$
PID (ProcessId) of shell itself
$!
PID of background process that shell runs last
$?
The end code of the last run command (return value)
$-
List of flags set with set command
$*
List of all parameters. For example, if “$*” is enclosed with “”, “1 $2” will be used as a reference Output all parameters in the form of $n “.
[email protected]
List of all parameters. If “[email protected]” is enclosed by “”, “1” $2 “…” Output all parameters in the form of “$n”.
$#
The number of parameters added to the shell
$0
The file name of the shell itself
$1~$n

The value of each parameter added to the shell. $1 is the first parameter and $2 is the second parameter.

Example:

1 #!/bin/bash
 2 #
 3 printf “The complete list is %s\n” “$$”
 4 printf “The complete list is %s\n” “$!”
 5 printf “The complete list is %s\n” “$?”
 6 printf “The complete list is %s\n” “$*”
 7 printf “The complete list is %s\n” “[email protected]
 8 printf “The complete list is %s\n” “$#”
 9 printf “The complete list is %s\n” “$0”
10 printf “The complete list is %s\n” “$1”
11 printf “The complete list is %s\n” “$2

result:

[[email protected] ~]$ bash params.sh 123456 QQ
The complete list is 24249
The complete list is
The complete list is 0
The complete list is 123456 QQ
The complete list is 123456
The complete list is QQ
The complete list is 2
The complete list is params.sh
The complete list is 123456
The complete list is QQ
Have a nice day!!!

The above article discusses the meaning of shell variables $#, [email protected], $0, $1 and $2 in Linux. The explanation is the whole content shared by Xiaobian. I hope it can give you a reference, and I hope you can support developeppaer more.