$0
表示所执行程序的路径名。
[huey@huey-K42JE ~]$ ll ~/bin total 4 -rwxrwxr-x 1 huey huey 21 Oct 24 14:39 hello [huey@huey-K42JE ~]$ cat ~/bin/hello #!/bin/bash echo '$0:' $0 [huey@huey-K42JE ~]$ hello /home/huey/bin/hello [huey@huey-K42JE ~]$
$n
表示传递给脚本或函数的第 n 个参数,如 $1 表示第一个参数。
[huey@huey-K42JE ~]$ ll ~/bin total 4 -rwxrwxr-x 1 huey huey 63 Oct 24 14:52 hello [huey@huey-K42JE ~]$ cat ~/bin/hello #!/bin/bash echo '$1:' $1 echo '$10:' $10 echo '${10}:' ${10} [huey@huey-K42JE ~]$ hello a b c d e f g h i j $1: a $10: a0 ${10}: j
注意:第 10 个参数,用 ${10} 表示,而不是 $10。
$#
表示传递给脚本或函数的参数个数。
[huey@huey-K42JE ~]$ ll ~/bin total 4 -rwxrwxr-x 1 huey huey 27 Oct 24 15:00 hello [huey@huey-K42JE ~]$ cat ~/bin/hello #!/bin/bash echo '$#:' $# [huey@huey-K42JE ~]$ hello $#: 0 [huey@huey-K42JE ~]$ hello a b c $#: 3
$*
表示传递给脚本或函数的所有参数。当包括在双引号内时,扩展为双引号引用的有全部位置参数构成的字符串,每个位置参数以 IFS 变量的第一个字符(默认情况下为空格)间隔开。
$@
表示传递给脚本或函数的所有参数。当包括在双引号内时,将每个位置参数扩展为双引号引用的单独单词。
[huey@huey-K42JE ~]$ ll ~/bin total 4 -rwxrwxr-x 1 huey huey 284 Oct 24 15:21 hello [huey@huey-K42JE ~]$ cat ~/bin/hello #!/bin/bash echo 'Print each param from $*:' for var in $* do echo $var done echo 'Print each param from $@:' for var in $@ do echo $var done echo 'Print each param from "$*":' for var in "$*" do echo $var done echo 'Print each param from "$@":' for var in "$@" do echo $var done [huey@huey-K42JE ~]$ hello a b c Print each param from $*: a b c Print each param from $@: a b c Print each param from "$*": a b c Print each param from "$@": a b c
$?
表示上个命令退出的状态或函数的返回值。
$$
表示当前 Shell 的进程 ID。
[huey@huey-K42JE cmdline]$ ps PID TTY TIME CMD 17379 pts/3 00:00:00 bash 18370 pts/3 00:00:00 ps [huey@huey-K42JE cmdline]$ echo $$ 17379