source $0 & bash $0
bash file.sh这种形式启动时,$0才被设置成文件名。
source是bash的内建命令,把文件读取到当前bash中执行,相当于在当前bash手动输入命令,所以$0是bash.
示例代码如下:
a.sh
echo "in a.sh. \$0 is: $0"
b.sh
echo "in b.sh. \$0 is: $0" . ./a.sh
运行如下:
lxw ~$ echo $0 bash lxw ~$ source a.sh in a.sh. $0 is: bash lxw ~$ source b.sh in b.sh. $0 is: bash in a.sh. $0 is: bash lxw ~$ bash a.sh in a.sh. $0 is: a.sh lxw ~$ bash b.sh in b.sh. $0 is: b.sh in a.sh. $0 is: b.sh lxw ~$ echo $0 bash lxw ~$
Reference:
linux shell中$0是什么意思: http://zhidao.baidu.com/link?url=5LneI7gPE4u59NHCQCuF6RJCwXyb3zkKtR4ZXiIdclE7zLtNmTug-8CAZFZko6oJFCkaC6Fl1dDjTyI1Ftu7aNkoZ22T6nzFIt-q4CuK8me