如何从 命令行 重新加载文件 .bash_profile?
我可以让 shell 通过退出并重新登录来识别对 .bash_profile 的更改,但我希望能够按需进行。
只需键入 source ~/.bash_profile
。
或者,如果您喜欢保存击键,您可以输入 . ~/.bash_profile
。
. ~/.bash_profile
只要确保您对那里的当前状态没有任何依赖关系。
.
命令是什么?
.
只是 source
命令的别名。
.
早于 source
。
source
是 .
的 bash 特定实现
.
和 source
在 bash 中实际上是相同的东西。来自链接:“source
是 bash 中点/句点 '.
' 的同义词,但在 POSIX sh 中不是,因此为了获得最大的兼容性,请使用句点。”
只需键入:
. ~/.bash_profile
但是,如果您希望它在终端启动时自动运行,而不是每次打开终端时都运行它,您可以将 . ~/.bash_profile
添加到 ~/.bashrc
文件。
笔记:
当您打开终端时,终端会以(非登录)交互模式启动 bash,这意味着它将 source ~/.bashrc
。
~/.bash_profile
仅在以交互式登录模式 启动时由 bash 提供。这通常仅在您登录控制台时(Ctrl+Alt+F1..F6),或通过 ssh
连接。
如果您不介意丢失当前 shell 终端的历史记录,您也可以这样做
bash -l
这将分叉你的 shell 并打开另一个 bash 子进程。 -l
参数告诉 Bash 作为登录 shell 运行。这是必需的,因为 .bash_profile 不会作为非登录 shell 运行。有关这方面的更多信息,read here。
如果你想完全替换当前的 shell,你也可以这样做:
exec bash -l
上面不会派生你当前的 shell,而是完全替换它,所以当你输入 exit
时,它将完全终止,而不是让你回到以前的 shell。
您还可以使用此命令为该用户重新加载 ~/.bash_profile。确保使用破折号。
su - username
我喜欢这样一个事实,即在您刚刚编辑文件之后,您需要做的就是输入:
. !$
这将获取您刚刚在历史记录中编辑的文件。请参阅What is bang dollar in bash。
.
" is an alias for "source
"。在 the man page 上,它位于当前 shell 环境中的 " 附近,并返回从 " 执行的最后一个命令的退出状态(尽管没有那么有用(太简洁))。
保存 .bash_profile 文件 通过键入 cd Reload the profile with 转到用户的主目录。 .bash_profile
cd
一起回家吧。不需要~
。
cd
- 您可以从当前所在的目录重新加载它:. ~/.bash_profile
将 alias bashs="source ~/.bash_profile"
添加到您的 Bash 文件中。
因此,您可以在下次调用 bashs
。
reset
- 更容易记住
利用
alias reload!=". ~/.bash_profile"
或者如果想通过函数添加日志:
function reload! () {
echo "Reloading bash profile...!"
source ~/.bash_profile
echo "Reloaded!!!"
}
. ~/. bash_profile
或 source ~/.bash_profile
如果 .bash_profile 文件不存在,可以尝试运行以下命令:
. ~/.bashrc
或者
source ~/.bashrc
而不是 .bash_profile。
您可以找到有关 bashrc 的更多信息。
虽然使用 source ~/.bash_profile
或以前的答案有效,但要提到的一件事是,这只会在您正在查看的当前选项卡或会话中重新加载您的 Bash 配置文件。如果您希望在每个选项卡/shell 上重新加载 bash 配置文件,则需要在每个选项卡/shell 中手动输入此命令。
如果您使用 iTerm, you can use CMD⌘ + Shift + I to enter a command into all current tabs。对于终端,引用此 issue 可能很有用;
我使用 Debian,我可以简单地输入 exec bash
来实现这一点。我不能说它是否适用于所有其他发行版。
. .bash_profile
即可完成这项工作。与 7urkm3n 上面给出的回复相同。
我正在运行 macOS v10.12 (Sierra) 并为此工作了一段时间(尝试了所有推荐的解决方案)。我变得困惑,所以我最终尝试重新启动我的电脑!有效。
我的结论是,有时硬重置是必要的。
简单地重新采购文件不会“重新加载”,因为某些东西首先被卸载,然后再次加载。如果这是你想要的,你可以这样做:
hash -r && _SHOW_MESSAGES=1 exec -a -bash bash
alias BASHRELOAD=". ~/.bash_profile"
怎么样。如果您经常这样做,您可以将其别名为br
。alias reload='source ~/.bash_profile && echo "File .bash_profile reloaded correctly" || echo "Syntax error, could not import the file"';