bash 如何在不注销并重新登录的情况下重新加载 .bashrc?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2518127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I reload .bashrc without logging out and back in?
提问by Jed Daniels
If I make changes to .bashrc
, how do I reload it without logging out and back in?
如果我对 进行了更改.bashrc
,如何在不注销并重新登录的情况下重新加载它?
回答by George Hawkins
You just have to enter the command:
你只需要输入命令:
source ~/.bashrc
or you can use the shorter version of the command:
或者您可以使用该命令的较短版本:
. ~/.bashrc
回答by WhoSayIn
Or you could use:
或者你可以使用:
exec bash
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 of bash
(with the updated configuration files).
该exec
命令通过运行指定的命令行完全替换 shell 进程。在我们的示例中,它将当前 shell 的任何内容替换为一个新实例bash
(使用更新的配置文件)。
回答by mklement0
To complement and contrastthe two most popular answers, . ~/.bashrc
and exec bash
:
为了补充和对比两个最流行的答案,. ~/.bashrc
并且exec bash
:
Both solutions effectivelyreload ~/.bashrc
, but there are differences:
两种解决方案都有效地重新加载~/.bashrc
,但存在差异:
. ~/.bashrc
orsource ~/.bashrc
will preserveyour current shell:- Except for the modifications that reloading
~/.bashrc
into the current shell (sourcing) makes, the current shell and its stateare preserved, which includes environment variables, shell variables, shell options, shell functions, and command history.
- Except for the modifications that reloading
exec bash
, or, more robustly,exec "$BASH"
[1], will replaceyour current shellwith a new instance, and therefore onlypreserve your current shell's environment variables(including ones you've defined ad-hoc).- In other words: Any ad-hoc changes to the current shell in terms of shell variables, shell functions, shell options, command history are lost.
. ~/.bashrc
或source ~/.bashrc
将保留您当前的外壳:- 除了修饰重装
~/.bashrc
到当前壳(采购)使得,在当前壳和它的状态被保存,包括环境变量,shell变量,shell选项,外壳的功能,和命令历史记录。
- 除了修饰重装
exec bash
,或者,更强大的是,exec "$BASH"
[1]将用新实例替换您当前的 shell,因此只保留您当前 shell 的环境变量(包括您已定义的临时变量)。- 换句话说:在 shell 变量、shell 函数、shell 选项、命令历史方面对当前 shell 的任何临时更改都将丢失。
Depending on your needs, one or the other approach may be preferred.
根据您的需要,可能首选一种或另一种方法。
[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 sameexecutable.
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 not likely in this case), you don't strictly need double quotes, but using them is a good habit to form.
[1]exec bash
理论上可以执行一个不同于bash
启动当前 shell 的可执行文件,如果它恰好存在于$PATH
. 由于特殊变量$BASH
始终包含启动当前 shell 的可执行文件的完整路径,exec "$BASH"
因此保证使用相同的可执行文件。
的说明重新"..."
周围$BASH
:双引号确保变量值被照原样使用,而不被解释击; 如果该值没有嵌入空格或其他 shell 元字符(在这种情况下不太可能),则您并不严格需要双引号,但使用它们是形成的好习惯。
回答by Randy Proctor
Someone edited my answer to add incorrect English, but here was the original, which is inferior to the accepted answer.
有人编辑了我的答案以添加不正确的英语,但这是原始答案,不如接受的答案。
. .bashrc
回答by Roy Lin
With this, you won'teven have to type "source ~/.bashrc":
有了这个,你就不会甚至要输入“源〜/ .bashrc中”:
Include your bashrc file:
包括你的 bashrc 文件:
alias rc="vim ~/.bashrc && source ~/.bashrc"
Every time you want to edit your bashrc, just run the alias "rc"
每次要编辑 bashrc 时,只需运行别名“ rc”
回答by James
Depending on your environment, just typing
根据您的环境,只需键入
bash
may also work.
也可以工作。
回答by Geoffrey Hale
. ~/.bashrc
Alternatives
备择方案
source ~/.bashrc
exec bash
exec
command replaces the shellwith a given program... – WhoSayIn
exec
命令用给定的程序替换外壳... – WhoSayIn
回答by karolus
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 run source ~/.bashrc
every time a session was opened, which doesn't help when running remote deploys.
根据您的环境,您可能希望添加脚本以在打开 SSH 会话时自动加载 .bashrc。我最近迁移到运行 Ubuntu 的服务器,默认情况下加载的是 .profile,而不是 .bashrc 或 .bash_profile。要在 .bashrc 中运行任何脚本,我必须在source ~/.bashrc
每次打开会话时运行,这在运行远程部署时无济于事。
To have your .bashrc load automatically when opening a session, try adding this to .profile:
要在打开会话时自动加载 .bashrc,请尝试将其添加到 .profile:
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
Reopen your session, and it should load any paths/scripts you have in .bashrc.
重新打开您的会话,它应该加载您在 .bashrc 中的所有路径/脚本。
回答by SherylHohman
I used easyengine to set up my vultr cloud based server.
I found my bash file at /etc/bash.bashrc
.
我使用 easyengine 来设置我的基于 vultr 云的服务器。
我在 .bashrc 找到了我的 bash 文件/etc/bash.bashrc
。
So source /etc/bash.bashrc
did the trick for me!
所以source /etc/bash.bashrc
奏效了我!
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.
设置裸服务器时(ubuntu 16.04),当你还没有设置用户名并且通过root登录时,你可以使用上面的信息。
It's best to create a user (with sudo privledges), and login as this username instead.
This will create a directory for your settings, including .profile and .bashrc files.
https://linuxize.com/post/how-to-create-a-sudo-user-on-ubuntu/
最好创建一个用户(使用 sudo privledges),并以此用户名登录。
这将为您的设置创建一个目录,包括 .profile 和 .bashrc 文件。
https://linuxize.com/post/how-to-create-a-sudo-user-on-ubuntu/
Now, you will edit and (and "source") the ~/.bashrc
file.
现在,您将编辑和(和“源”)~/.bashrc
文件。
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)
在我的服务器上,它位于/home/your_username/.bashrc
(your_username
实际上是您在上面创建的新用户名,现在登录)
回答by Jimmy MG Lim
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.
exec bash 是重新执行和启动新 shell 以替换当前 shell 的好方法。只是为了添加答案,$SHELL 返回当前的 shell,即 bash。通过使用以下命令,它将重新加载当前的 shell,而不仅仅是 bash。
exec $SHELL -l;
exec $SHELL -l;