如何在不注销并重新登录的情况下重新加载 .bashrc 设置?
如果我对 .bashrc
进行更改,如何在不注销并重新登录的情况下重新加载它?
If I make changes to .bashrc
, how do I reload it without logging out and back in?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
您可以输入长格式命令:
或者您可以使用该命令的较短版本:
You can enter the long form command:
or you can use the shorter version of the command:
或者你可以使用:
这做同样的事情,并且更容易记住(至少对我来说)。
exec
命令通过运行指定的命令行来完全替代 shell 进程。在我们的示例中,它将用新的 bash 实例(带有更新的配置文件)替换当前的 shell。Or you could use:
This does the same thing, and is easier to remember (at least for me).
The
exec
command completely replaces the shell process by running the specified command-line. In our example, it replaces whatever the current shell is with a fresh instance ofbash
(with the updated configuration files).为了补充和对比两个最流行的答案,
。 ~/.bashrc
和exec bash
:两种解决方案< em>有效地重新加载
~/.bashrc
,但有区别:。 ~/.bashrc
或source ~/.bashrc
将保留您当前的 shell 会话:~/.bashrc
重新加载到当前 shell(采购)所做的修改之外,当前 shell 进程及其状态< /em> 保留,其中包括环境变量、shell 变量、shell 选项、shell 函数和命令历史记录。exec bash
,或者更可靠的是,exec "$BASH"
[1],将替换您当前的 shell为新实例,因此仅保留当前 shell 的环境变量 (包括您在会话中临时定义的)。
根据您的需求,可能会首选其中一种方法。
注意:上述内容也类似地适用于其他 shell:
exec
方法应用于无论您的默认 shell 是什么,请使用exec $ SHELL
zsh
:。 ~/.zshrc
[1]
exec bash
理论上可以执行与启动的可执行文件不同的bash
可执行文件当前 shell(如果它恰好存在于$PATH
前面列出的目录中)。由于特殊变量$BASH
始终包含启动当前 shell 的可执行文件的完整路径,因此exec "$BASH"
保证使用相同 可执行文件。围绕
$BASH
的"..."
注释:双引号确保变量值按原样使用,无需由 Bash 解释;如果该值没有嵌入空格或其他 shell 元字符(在本例中很可能如此),则您并不严格需要双引号,但使用它们是一个好习惯。To complement and contrast the two most popular answers,
. ~/.bashrc
andexec bash
:Both solutions effectively reload
~/.bashrc
, but there are differences:. ~/.bashrc
orsource ~/.bashrc
will preserve your current shell session:~/.bashrc
into the current shell (sourcing) makes, the current shell process and its state are preserved, which includes environment variables, shell variables, shell options, shell functions, and command history.exec bash
, or, more robustly,exec "$BASH"
[1],will replace your current shell with a new instance, and therefore only preserve your current shell's environment variables (including ones you've defined ad hoc, in-session).
Depending on your needs, one or the other approach may be preferred.
Note: The above applies analogously to other shells too:
exec
approach to whatever your default shell is, useexec $SHELL
zsh
:. ~/.zshrc
[1]
exec bash
could in theory execute a differentbash
executable than the one that started the current shell, if it happens to exist in a directory listed earlier in the$PATH
. Since special variable$BASH
always contains the full path of the executable that started the current shell,exec "$BASH"
is guaranteed to use the same executable.A note re
"..."
around$BASH
: double-quoting ensures that the variable value is used as-is, without interpretation by Bash; if the value has no embedded spaces or other shell metacharacters (which is likely in this case), you don't strictly need double quotes, but using them is a good habit to form.有人编辑了我的答案,添加了不正确的英文,但这是原文,它不如公认的答案。
Someone edited my answer to add incorrect English, but here was the original, which is inferior to the accepted answer.
有了这个,您甚至不必输入“source ~/.bashrc”:
包含您的 bashrc 文件:
每次您想要编辑 bashrc 时,只需运行别名“rc”
With this, you won't even have to type "source ~/.bashrc":
Include your bashrc file:
Every time you want to edit your bashrc, just run the alias "rc"
根据您的环境,仅键入
也可能有效。
Depending on your environment, just typing
may also work.
替代方案
Alternatives
我使用 easyengine 来设置我的 vultr 基于云的服务器。
我在
/etc/bash.bashrc
找到了我的 bash 文件。所以
source /etc/bash.bashrc
帮我解决了这个问题!更新
当设置裸服务器(ubuntu 16.04)时,如果您尚未设置用户名并通过root登录,则可以使用上述信息。
最好创建一个用户(具有 sudo 权限)< /a>,然后以此用户名登录。
这将为您的设置创建一个目录,包括
.profile
和.bashrc
文件,如上一个资源中所述。现在,您将编辑(和
source
)~/.bashrc
文件。在我的服务器上,它位于
/home/your_username/.bashrc
(其中
your_username
实际上是您在上面创建的新用户名,现在使用该用户名登录)I used easyengine to set up my vultr cloud based server.
I found my bash file at
/etc/bash.bashrc
.So
source /etc/bash.bashrc
did the trick for me!update
When setting up a bare server (ubuntu 16.04), you can use the above info, when you have not yet set up a username, and are logging in via root.
It's best to create a user (with sudo privileges), and login as this username instead.
This will create a directory for your settings, including
.profile
and.bashrc
files as described on the previous ressource.Now, you will edit and (and
source
) the~/.bashrc
file.On my server, this was located at
/home/your_username/.bashrc
(where
your_username
is actually the new username you created above, and now login with)exec bash
是重新执行和启动新 shell 来替换当前 shell 的好方法。只是为了添加答案,$SHELL
返回当前的 shell,即 bash。通过使用以下命令,它将重新加载当前 shell,而不仅仅是 bash。exec bash
is a great way to re-execute and launch a new shell to replace current. just to add to the answer,$SHELL
returns the current shell which is bash. By using the following, it will reload the current shell, and not only to bash.根据您的环境,您可能需要添加脚本以便在打开 SSH 会话时自动加载
.bashrc
。我最近迁移到运行 Ubuntu 的服务器,默认加载.profile
,而不是.bashrc
或.bash_profile
。要运行.bashrc
中的任何脚本,我必须在每次打开会话时运行source ~/.bashrc
,这在运行远程部署时没有帮助。要在打开会话时自动加载
.bashrc
,请尝试将其添加到.profile
:重新打开会话,它应该加载您在
中的所有路径/脚本>.bashrc
。Depending upon your environment, you may want to add scripting to have
.bashrc
load automatically when you open an SSH session. I recently did a migration to a server running Ubuntu, and there,.profile
, not.bashrc
or.bash_profile
is loaded by default. To run any scripts in.bashrc
, I had to runsource ~/.bashrc
every time a session was opened, which doesn't help when running remote deploys.To have your
.bashrc
load automatically when opening a session, try adding this to.profile
:Reopen your session, and it should load any paths/scripts you have in
.bashrc
.对我来说,更改路径时有效的是:
exec "$BASH" --login
For me what works when I change the PATH is:
exec "$BASH" --login
上使用以下命令
我在 msysgit的较短版本
i use the following command on msysgit
shorter version of
假设有一个交互式 shell,您希望保留当前的命令历史记录并加载 /etc/profile(它加载包括 /etc/bashrc 在内的环境数据,在 Mac OS X 上加载 /etc/paths.d/ 中定义的路径) path_helper),附加您的命令历史记录并使用登录('-l')选项执行 bash:
Assuming an interactive shell, and you'd like to keep your current command history and also load /etc/profile (which loads environment data including /etc/bashrc and on Mac OS X loads paths defined in /etc/paths.d/ via path_helper), append your command history and do an exec of bash with the login ('-l') option:
我知道您在注销并再次登录后需要一个 shell。
我相信实现这一目标的最佳方法是:
exec
将替换当前的 shell,这样当新的 shell 退出时,您就不会剩下它。env
将创建一个新的空环境,使用-i
我们添加$HOME
以便您的 shell(通常是 bash)由$ 给出SHELL
可以找到~/.profile
/~/.bash_profile
(因此(在 ubuntu 上或如果指定)~/.bashrc
)。这些将通过-l
获得。但我并不完全确定。I understand you want a shell as after logging out and in again.
I believe the best way to achieve that is:
exec
will replace the current shell, such that you are not left with it when the new one exits.env
will create a new empty environment, with-i
we add$HOME
so that your shell (usually bash) given by$SHELL
can find~/.profile
/~/.bash_profile
(and thus (on ubuntu or if specified)~/.bashrc
). Those will be sourced thanks to-l
. I'm not completely sure though.请注意 $SHELL 可能会产生意外结果
例如,在 Docker 环境上连接,
因此如果您尝试,您将断开连接,
因此您可能必须使用更复杂的东西,例如
假设每个 shell 都包含“sh”,并且此命令管道
会生成完整的命令
如果您有参数或标志,您可能必须使用 -f2,3,4
或尝试
但请阅读上面的所有建议
就我个人而言,我不喜欢丢失 shell 历史记录...但这取决于您和您的需求
Be aware of $SHELL may produce unexpected results
for example connected on a Docker environment
so if you try, you will be disconnected
so you may have to use something more complicated like
assuming that every shell contains "sh", and this command pipeline
produces a complete command
if you have arguments or flags you may have to use -f2,3,4
or try
But read all the advices above
Personally i dont like to loose the shell history... but it's up to you and to your needs
我注意到纯 exec bash 命令将保留环境变量,因此您需要使用 exec -c bash 在空环境中运行 bash。
例如,您登录 bash,然后
export A=1
,如果您exec bash
,则A == 1
。如果您
exec -cl bash
,则A
为空。我认为这是完成工作的最佳方式。
I noticed that pure
exec bash
command will preserve the environment variables, so you need to useexec -c bash
to run bash in an empty environment.For example, you login a bash, and
export A=1
, if youexec bash
, theA == 1
.If you
exec -cl bash
,A
is empty.I think this is the best way to do your job.
这也将起作用..
This will also work..
我编写了一组名为 bash_magic 的脚本,可以跨多个 shell 自动执行此过程。如果您更新 bash magic shell 目录(默认为
.bash.d
)中的 shell 文件,它将在下一个提示时自动获取更新。因此,一旦您进行了更改,只需按Enter
/return
键即可获取所有更新。I wrote a set of scripts I called bash_magic that automates this process across numerous shells. If you update a shell file in the bash magic shell directory (
.bash.d
by default), it will automatically source the update at the next prompt. So once you've made a change, just hit theEnter
/return
key and any updates will be sourced.