Shell 特殊变量
特殊变量
$$
当前 shell 的进程ID
1 | echo $$ |
输出当前进程的pid
1 | ➜ ~ echo $$ |
| $0 | 当前脚本的文件名 |
|---|---|
| $n | 当前脚本的参数,从1开始 $1第一个参数,$2第二个参数,$n第n个参数 |
| $# | 脚本参数的总个数 |
| $* | 脚本参数集合,所有的参数被双引号包裹。如果脚本2个参数,$*="$1 $2" |
| $@ | 脚本参数集合,每个参数被单独的双引号包裹。如果脚本2个参数,$*="$1" "$2" |
| $? | 上个命令的返回值。 |
| $$ | 当前shell 的进程ID。 |
| $! | The process number of the last background command. |
示例
1 | !/bin/sh |
输出
1 | ./test.sh Zara Ali |
$* 和$@
the $* special parameter takes the entire list as one argument with spaces between and the $@ special parameter takes the entire list and separates it into separate arguments.
1 | !/bin/sh |
输出
1 | ./test.sh Zara Ali 10 Years Old |
$?
检查上个命令是否执行成功。
1 | ➜ ~ pwd |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 yxibng!
