shell下以美元符$开头的环境变量
shell脚本中常见的以美元符开头的环境变量,有的时候英文描述好像比中文更贴切一些。
大家直接看英文吧。
$n $1 the first parameter,$2 the second...
$# the number of command-line parameters.
$0 the name of current program.
$? last command or function's return value.
$$ the program's PID.
$! last program's PID.
$@ save all the parameters.
看完说明举个实际例子才能更好的理解
1 2 3 4 5 6 7 8 |
#!/bin/bash echo "the number of command-line parameters: $#" echo "the first parameter: $1" echo "the name of current program: $0" echo "last command or function's return value: $?" echo "the programs PID: $$" echo "last program's PID: $!" echo "all the parameters: $@" |
保存如上内容为testenv.sh,然后在命令行执行
$ chmod a+x testenv.sh
$ ./testenv.sh first second
the number of command-line parameters: 2
the first parameter: first
the name of current program: ./testenv.sh
last command or function's return value: 0
the programs PID: 10688
last program's PID:
all the parameters: first second