Bash 中的 $0 在什么时候不是 argv[0]
2015-11-27 11:32
489 查看
每个 C 程序都有一个 main 函数,每个 main 函数都有一个 argv 参数,这个参数是一个字符串数组,这个数组的值是由该 C 程序的父进程在通过 exec* 函数启动它时指定的。
很多人说 Bash 中的 $0 的值就是 bash 这个 C 程序在它的 main 函数中获取到的 argv[0](zeroth argument)的值,我们可以通过 exec 命令的 -a 参数的功能演示一下:
但并不都是这样,在两种情况下,$0 的值不是 argv[0]:
这个时候 bash 程序的 argv[0] 是 “bash”,但 $0 却是 “foo”。也就是说如果 -c 选项的参数后面还有参数,那么那些参数会依次成为 $0(覆盖了旧的值 argv[0])、$1、$2...。
这个时候 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),
很多人说 Bash 中的 $0 的值就是 bash 这个 C 程序在它的 main 函数中获取到的 argv[0](zeroth argument)的值,我们可以通过 exec 命令的 -a 参数的功能演示一下:
$ ( exec -a foo bash -c 'echo $0' ) foo $ ( exec -a ... bash -c 'echo $0' ) ... $ ( exec -a "" bash -c 'echo $0' ) |
bash -c '...' foo bar ...
$ bash -c 'echo $0 $1' foo bar foo bar |
bash /a/b/c.sh
$ cat foo.sh echo $0 $ bash foo.sh foo.sh $ bash ./foo.sh ./foo.sh $ ./foo.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),
$0is set to the name of that file. If Bash is started with the -c option (see Invoking Bash), then
$0is 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.
相关文章推荐
- shell特性、shell变量、shell通配符
- 一对简单的 reverse shell / shell
- shell编程学习
- shell变量
- shell多线程
- shell简单的发布脚本
- shell 快速浏览
- Zookeeper shell操作
- 【转】终极 Shell
- Shell编程——正则表达式基础与扩展
- shell 一些例子
- 内网***利器--reDuh(webshell跳板)简单使用说明
- spark cluster 下 spark-shell/spark-spark提交wordcount sparksql Demo
- Shell学习笔记 - 条件判断式
- bash (cmd )管道和重定向
- 程序调用ShellExecuteEx打开其他程序(兼容UAC获取管理员权限)
- delphi 打开文件夹并定位到一个文件(使用ShellExecute时加一个select参数,原来这么简单!)
- shell判断文件,目录是否存在或者具有权限的代码
- debain的useradd默认没有主目录及shell
- shell 下数值、字符串比较