【发布时间】:2023-03-30 06:36:01
【问题描述】:
这是一个将 bash 文件移动到主页并使用源命令加载它的脚本。
# update.sh
#!/bin/bash
cp -f $PWD/bash_profile ~/.bash_profile
source ~/.bash_profile
它不起作用!它使用cp -f $PWD/bash_profile ~/.bash_profile 更新文件。
在~/.bash_profile 中有一个新的 PS1 定义。文件已更新,但在打开新窗口之前没有发生任何更改。我需要在脚本执行后运行source ~/.bash_profile ...
是否可以在 bash 脚本中运行 source 命令?
【问题讨论】:
-
如果你想更新你当前的 shell,你需要获取 bash 脚本。
-
这是可能的,但由于在脚本中使用
source而不是执行.bash_profile的原因相同,但没用。 -
shebang 必须是文件的第一行。
-
不是一个新的答案,而是一些澄清。当你
echo "PS1=${PS1}" aftersource ~/.bash_profile, you will see it is changed. The changes get lost, when your scriptupdate.sh` 完成。您可以使用source使更改在当前 shell 中保持有效。
标签: bash dot-source