如何从命令行重新加载 .bash_profile ?我可以让shell通过退出并重新登录来识别对 .bash_profile 的更改,但我希望能够按需执行此操作 .
.bash_profile
保存 .bash_profile 文件
输入 cd 转到用户的主目录
cd
使用 . .bash_profile 重新加载配置文件
. .bash_profile
只需输入 source ~/.bash_profile
source ~/.bash_profile
或者,如果您想保存击键,可以输入 . ~/.bash_profile
. ~/.bash_profile
我喜欢这样的事实:在您刚编辑完文件后,您需要做的就是输入:
. !$
这将获取您刚刚在历史记录中编辑的文件 . 见What is bang dollar in bash .
我使用Debian,我只需输入 exec bash 即可实现此目的 . 我不能说它是否适用于所有其他发行版 .
exec bash
你只需输入 . ~/.bash_profile
参考:https://superuser.com/questions/46139/what-does-source-do
只需确保您对当前状态没有任何依赖关系 .
我正在运行Sierra,并且正在研究这一段时间(尝试所有推荐的解决方案) . 我变得困惑,所以最终尝试重新启动我的电脑!有效
我的结论是,有时需要进行硬重置
我想发布一个快速回答,即在使用 source ~/.bash_profile 或上面提到的答案时,有一点需要注意的是,这只会在您正在查看的当前标签或会话中重新加载您的bash配置文件 . 如果要在每个选项卡/ shell上重新加载bash配置文件,则需要在每个选项卡/ shell中手动输入此命令 .
如果你使用iTerm, you can use CMD⌘+Shift+I to enter a command into all current tabs . 对于终端,参考这个issue可能是有用的;
如果您不介意丢失当前shell终端的历史记录,您也可以这样做
bash -l
这会分叉你的shell并打开另一个bash的子进程 . -l 参数告诉bash作为登录shell运行,这是必需的,因为.bash_profile不会作为非登录shell运行,有关此更多信息read here
-l
如果要完全替换当前shell,还可以执行以下操作:
exec bash -l
上面的内容不会分叉当前的shell,而是完全替换它,因此当你输入 exit 时它会完全终止,而不是让你掉到上一个shell .
exit
如果.bash_profile不存在,您可以尝试运行以下命令:
. ~/.bashrc
要么
source ~/.bashrc
而不是.bash_profile . 你可以找到更多关于bashrc的信息
alias reload!=". ~/.bash_profile"
或者如果想通过函数添加日志
function reload! () { echo "Reloading bash profile...!" source ~/.bash_profile echo "Reloaded!!!" }
将 alias bashs="source ~/.bash_profile" 添加到您的bash文件中 . 所以你可以从下次打电话 bashs
alias bashs="source ~/.bash_profile"
bashs
只需输入:
但是,如果要在终端启动时自动运行它而不是每次打开终端时都运行它,则可以将 . ~/.bash_profile 添加到 ~/.bashrc 文件中 .
~/.bashrc
Note:
当您打开终端时,终端以(非登录)交互模式启动bash,这意味着它将来源 ~/.bashrc .
~/.bash_profile 仅在 interactive login mode 中启动时由bash提供 . 这通常仅在您在控制台登录(Ctrl Alt F1..F6)或通过 ssh 连接时 .
~/.bash_profile
ssh
您也可以使用此命令为该用户重新加载〜/ .bash_profile . 确保使用短划线 .
su - username
14 回答
保存 .bash_profile 文件
输入
cd
转到用户的主目录使用
. .bash_profile
重新加载配置文件只需输入
source ~/.bash_profile
或者,如果您想保存击键,可以输入
. ~/.bash_profile
我喜欢这样的事实:在您刚编辑完文件后,您需要做的就是输入:
这将获取您刚刚在历史记录中编辑的文件 . 见What is bang dollar in bash .
我使用Debian,我只需输入
exec bash
即可实现此目的 . 我不能说它是否适用于所有其他发行版 .你只需输入
. ~/.bash_profile
参考:https://superuser.com/questions/46139/what-does-source-do
只需确保您对当前状态没有任何依赖关系 .
我正在运行Sierra,并且正在研究这一段时间(尝试所有推荐的解决方案) . 我变得困惑,所以最终尝试重新启动我的电脑!有效
我的结论是,有时需要进行硬重置
我想发布一个快速回答,即在使用
source ~/.bash_profile
或上面提到的答案时,有一点需要注意的是,这只会在您正在查看的当前标签或会话中重新加载您的bash配置文件 . 如果要在每个选项卡/ shell上重新加载bash配置文件,则需要在每个选项卡/ shell中手动输入此命令 .如果你使用iTerm, you can use CMD⌘+Shift+I to enter a command into all current tabs . 对于终端,参考这个issue可能是有用的;
如果您不介意丢失当前shell终端的历史记录,您也可以这样做
bash -l
这会分叉你的shell并打开另一个bash的子进程 .
-l
参数告诉bash作为登录shell运行,这是必需的,因为.bash_profile不会作为非登录shell运行,有关此更多信息read here如果要完全替换当前shell,还可以执行以下操作:
exec bash -l
上面的内容不会分叉当前的shell,而是完全替换它,因此当你输入
exit
时它会完全终止,而不是让你掉到上一个shell .如果.bash_profile不存在,您可以尝试运行以下命令:
要么
而不是.bash_profile . 你可以找到更多关于bashrc的信息
或者如果想通过函数添加日志
将
alias bashs="source ~/.bash_profile"
添加到您的bash文件中 . 所以你可以从下次打电话bashs
只需输入:
但是,如果要在终端启动时自动运行它而不是每次打开终端时都运行它,则可以将
. ~/.bash_profile
添加到~/.bashrc
文件中 .Note:
当您打开终端时,终端以(非登录)交互模式启动bash,这意味着它将来源
~/.bashrc
.~/.bash_profile
仅在 interactive login mode 中启动时由bash提供 . 这通常仅在您在控制台登录(Ctrl Alt F1..F6)或通过ssh
连接时 .您也可以使用此命令为该用户重新加载〜/ .bash_profile . 确保使用短划线 .