每一个 C 程序都有一个 main 函数,每一个 main 函数都有一个 argv 参数,这个参数是一个字符串数组,这个数组的值是由该 C 程序的父进程在经过 exec* 函数启动它时指定的。html
不少人说 Bash 中的 $0 的值就是 bash 这个 C 程序在它的 main 函数中获取到的 argv[0](zeroth argument)的值,咱们能够经过 exec 命令的 -a 参数的功能演示一下:shell
$ ( exec -a foo bash -c 'echo $0' )数组 foobash $ ( exec -a ... bash -c 'echo $0' )函数 ...spa $ ( exec -a "" bash -c 'echo $0' )操作系统
|
但并不都是这样,在两种状况下,$0 的值不是 argv[0]:命令行
$ bash -c 'echo $0 $1' foo bar code foo barhtm |
这个时候 bash 程序的 argv[0] 是 “bash”,但 $0 倒是 “foo”。也就是说若是 -c 选项的参数后面还有参数,那么那些参数会依次成为 $0(覆盖了旧的值 argv[0])、$一、$2...。
$ cat foo.sh echo $0 $ bash foo.sh foo.sh $ bash ./foo.sh ./foo.sh $ ./foo.sh ./foo.sh |
这个时候 bash 程序的 argv[0] 仍是 “bash”,但 $0 倒是 “foo.sh”。也就是说,当执行一个脚本时,$0 的值就是那个脚本的相对或绝对路径(你指定的任意合法路径)。你在命令行中输入 ./foo.sh 也同样,由于操做系统会为你执行 /bin/sh ./foo.sh。
关于 $0 的值的这三种状况,Bash 文档其实都有讲,我分别用三种颜色标注相关话语:
($0) Expands to the name of the shell or shell script. This is set at shell initialization. If Bash is invoked with a file of commands (see Shell Scripts),
$0
is set to the name of that file. If Bash is started with the -c option (see Invoking Bash), then$0
is set to the first argument after the string to be executed, if one is present. Otherwise, it is set to the filename used to invoke Bash, as given by argument zero.