首页 > 代码库 > source $0 & bash $0

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 $0bashlxw ~$ source a.shin a.sh. $0 is: bashlxw ~$ source b.shin b.sh. $0 is: bashin a.sh. $0 is: bashlxw ~$ bash a.sh in a.sh. $0 is: a.shlxw ~$ bash b.sh in b.sh. $0 is: b.shin a.sh. $0 is: b.shlxw ~$ echo $0bashlxw ~$ 

 

Reference:

linux shell中$0是什么意思: http://zhidao.baidu.com/link?url=5LneI7gPE4u59NHCQCuF6RJCwXyb3zkKtR4ZXiIdclE7zLtNmTug-8CAZFZko6oJFCkaC6Fl1dDjTyI1Ftu7aNkoZ22T6nzFIt-q4CuK8me

source $0 & bash $0