我在 shell 脚本中有以下行:
source bash_profile
它没有错误,但它也没有做任何我能看到的事情(它没有像我期望的那样重新加载我的别名)
但是,从 shell 脚本所在的同一文件夹中,如果我只是从 shell 运行命令,它会像我预期的那样工作。
我也从不在脚本中更改目录。事实上,这是脚本:
direction="to"
destination="local"
if [ -n $1 ]
then
direction=$1
fi
if [ -n $2 ]
then
destination=$2
fi
command=$direction$destination
if [ $command = "fromlocal" -o $command = "togit" ]
then
cp /c/Program\ Files/Git/etc/bash_profile /d/automata/flgitscripts/bash_profile
else
cp /d/automata/flgitscripts/bash_profile /c/Program\ Files/Git/etc/bash_profile
fi
source /c/Program\ Files/Git/etc/bash_profile
我使用脚本来管理对 bash_profile 的更改,并将它们存储在 git 存储库中。
为什么 source 命令不能在 shell 脚本中运行?
请您参考如下方法:
您是否也在采购脚本:source thescriptname
?否则,脚本将在新的 shell 中运行,并且 source bash_profile
命令会影响新的 shell(在脚本结束时退出)。