如何在不注销并重新登录的情况下重新加载 .bashrc 设置?

发布于 2024-08-26 23:23:21 字数 59 浏览 4 评论 0原文

如果我对 .bashrc 进行更改,如何在不注销并重新登录的情况下重新加载它?

If I make changes to .bashrc, how do I reload it without logging out and back in?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(18

蓬勃野心 2024-09-02 23:23:21

您可以输入长格式命令:

source ~/.bashrc

或者您可以使用该命令的较短版本:

. ~/.bashrc

You can enter the long form command:

source ~/.bashrc

or you can use the shorter version of the command:

. ~/.bashrc
本宫微胖 2024-09-02 23:23:21

或者你可以使用:

exec bash

这做同样的事情,并且更容易记住(至少对我来说)。

exec 命令通过运行指定的命令行来完全替代 shell 进程。在我们的示例中,它将用新的 bash 实例(带有更新的配置文件)替换当前的 shell。

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).

っ〆星空下的拥抱 2024-09-02 23:23:21

为了补充和对比两个最流行的答案,。 ~/.bashrcexec bash

两种解决方案< em>有效地重新加载~/.bashrc,但有区别:

  • 。 ~/.bashrcsource ~/.bashrc保留您当前的 shell 会话

    • 除了将~/.bashrc重新加载到当前 shell(采购)所做的修改之外,当前 shell 进程及其状态< /em> 保留,其中包括环境变量、shell 变量、shell 选项、shell 函数和命令历史记录。


  • exec bash,或者更可靠的是,exec "$BASH"[1]
    替换您当前的 shell为新实例,因此保留当前 shell 的环境变量 (包括您在会话中临时定义的)。

    • 换句话说:对当前 shell 在 shell 变量、shell 函数、shell 选项、命令历史记录方面的任何临时更改都会丢失。

根据您的需求,可能会首选其中一种方法。


注意:上述内容也类似地适用于其他 shell

  • 要将 exec 方法应用于无论您的默认 shell 是什么,请使用 exec $ SHELL
  • 类似地,采购方法要求您知道并指定特定于 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 and exec bash:

Both solutions effectively reload ~/.bashrc, but there are differences:

  • . ~/.bashrc or source ~/.bashrc will preserve your current shell session:

    • Except for the modifications that reloading ~/.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).

    • In other words: Any ad-hoc changes to the current shell in terms of shell variables, shell functions, shell options, command history are lost.

Depending on your needs, one or the other approach may be preferred.


Note: The above applies analogously to other shells too:

  • To apply the exec approach to whatever your default shell is, use exec $SHELL
  • Similarly, the sourcing approach requires you to know and specify the name of the shell-specific initialization file; e.g., for zsh: . ~/.zshrc

[1] exec bash could in theory execute a different bash 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.

离笑几人歌 2024-09-02 23:23:21

有人编辑了我的答案,添加了不正确的英文,但这是原文,它不如公认的答案。

. .bashrc

Someone edited my answer to add incorrect English, but here was the original, which is inferior to the accepted answer.

. .bashrc
夏雨凉 2024-09-02 23:23:21

有了这个,您甚至不必输入“source ~/.bashrc”:

包含您的 bashrc 文件:

alias rc="vim ~/.bashrc && source ~/.bashrc"

每次您想要编辑 bashrc 时,只需运行别名“rc

With this, you won't even have to type "source ~/.bashrc":

Include your bashrc file:

alias rc="vim ~/.bashrc && source ~/.bashrc"

Every time you want to edit your bashrc, just run the alias "rc"

夜光 2024-09-02 23:23:21

根据您的环境,仅键入

bash

也可能有效。

Depending on your environment, just typing

bash

may also work.

吻风 2024-09-02 23:23:21
. ~/.bashrc

. is a POSIX-mandated builtin


Alternatives

source ~/.bashrc

source is a synonym for dot/period . in bash, but not in POSIX sh, so for maximum compatibility use the period.

exec bash
  • exec command replaces the shell with a given program... – WhoSayIn
清泪尽 2024-09-02 23:23:21

我使用 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)

同尘 2024-09-02 23:23:21

exec bash 是重新执行和启动新 shell 来替换当前 shell 的好方法。只是为了添加答案,$SHELL 返回当前的 shell,即 bash。通过使用以下命令,它将重新加载当前 shell,而不仅仅是 bash。

exec $SHELL -l;

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 $SHELL -l;
北城挽邺 2024-09-02 23:23:21

根据您的环境,您可能需要添加脚本以便在打开 SSH 会话时自动加载 .bashrc。我最近迁移到运行 Ubuntu 的服务器,默认加载 .profile,而不是 .bashrc.bash_profile。要运行 .bashrc 中的任何脚本,我必须在每次打开会话时运行 source ~/.bashrc,这在运行远程部署时没有帮助。

要在打开会话时自动加载 .bashrc,请尝试将其添加到 .profile

if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

重新打开会话,它应该加载您在 中的所有路径/脚本>.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 run source ~/.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:

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.

愁以何悠 2024-09-02 23:23:21

对我来说,更改路径时有效的是:exec "$BASH" --login

For me what works when I change the PATH is: exec "$BASH" --login

往事随风而去 2024-09-02 23:23:21

上使用以下命令

. ~/.bashrc

我在 msysgit的较短版本

source ~/.bashrc

i use the following command on msysgit

. ~/.bashrc

shorter version of

source ~/.bashrc
从此见与不见 2024-09-02 23:23:21

假设有一个交互式 shell,您希望保留当前的命令历史记录并加载 /etc/profile(它加载包括 /etc/bashrc 在内的环境数据,在 Mac OS X 上加载 /etc/paths.d/ 中定义的路径) path_helper),附加您的命令历史记录并使用登录('-l')选项执行 bash:

history -a && exec bash -l

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:

history -a && exec bash -l
小傻瓜 2024-09-02 23:23:21

我知道您在注销并再次登录后需要一个 shell。
我相信实现这一目标的最佳方法是:

exec env -i HOME="$HOME" "$SHELL" -l

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 env -i HOME="$HOME" "$SHELL" -l

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.

各自安好 2024-09-02 23:23:21

请注意 $SHELL 可能会产生意外结果
例如,在 Docker 环境上连接,

echo $SHELL
/usr/sbin/nologin

因此如果您尝试,您将断开连接,

exec $SHELL
This account is currently not available.

因此您可能必须使用更复杂的东西,例如

exec $(pgrep -l sh | grep "^`echo $` " | cut -d" " -f2)

假设每个 shell 都包含“sh”,并且此命令管道

pgrep -l sh | grep "^`echo $` " | cut -d" " -f2

会生成完整的命令
如果您有参数或标志,您可能必须使用 -f2,3,4
或尝试

pgrep -l sh | grep "^`echo $` " | sed -E 's/^[0-9]+ //'

但请阅读上面的所有建议
就我个人而言,我不喜欢丢失 shell 历史记录...但这取决于您和您的需求

Be aware of $SHELL may produce unexpected results
for example connected on a Docker environment

echo $SHELL
/usr/sbin/nologin

so if you try, you will be disconnected

exec $SHELL
This account is currently not available.

so you may have to use something more complicated like

exec $(pgrep -l sh | grep "^`echo $` " | cut -d" " -f2)

assuming that every shell contains "sh", and this command pipeline

pgrep -l sh | grep "^`echo $` " | cut -d" " -f2

produces a complete command
if you have arguments or flags you may have to use -f2,3,4
or try

pgrep -l sh | grep "^`echo $` " | sed -E 's/^[0-9]+ //'

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

北音执念 2024-09-02 23:23:21

我注意到纯 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 use exec -c bash to run bash in an empty environment.

For example, you login a bash, and export A=1, if you exec bash, the A == 1.

If you exec -cl bash, A is empty.

I think this is the best way to do your job.

相思故 2024-09-02 23:23:21

这也将起作用..

cd ~
source .bashrc

This will also work..

cd ~
source .bashrc
枕头说它不想醒 2024-09-02 23:23:21

我编写了一组名为 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 the Enter/return key and any updates will be sourced.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文