Shell $0

我们已经知道在Shell中$0表示Shell脚本的文件名,但在有脚本调用的情形中,子脚本中的$0会是什么值呢?我们通过下面的实例来看。

已测试系统列表:

  • Mac OS X EI Capitan 10.11.6
  • Ubuntu 16.04 LTS (GNU/Linux 4.4.0-28-generic x86_64)

父脚本a.sh,位置test1/a.sh,内容如下:

1
2
3
4
!/usr/bin/env bash

echo "$0"
. ../test2/b.sh

子脚本b.sh,位置test2/b.sh,内容如下:

1
2
3
#!/usr/bin/env bash

echo "the sub script is: $0"

此时执行父脚本输出结果是:

1
2
3
$ sh a.sh
the main script is a.sh
the sub script is: a.sh

如果父脚本内容如下:

1
2
3
4
#!/usr/bin/env bash

echo "the main script is $0"
../test2/b.sh

则输出结果为:

1
2
3
$ sh a.sh 
the main script is a.sh
the sub script is: ../test2/b.sh

可见,在父脚本中调用子脚本的不同,在子脚本中$0的值也不同。至于为什么会这样本人需要继续学习以找到答案。

测试过程中注意给脚本赋可执行权限。