Linux 如何让 GNU 屏幕读取 .bash_profile/.bash_rc 更改?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6945534/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:28:14  来源:igfitidea点击:

How do I get GNU screen to read .bash_profile/.bash_rc changes?

linuxbashunixgnu-screen.bash-profile

提问by Francis Lewis

After I make changes in .bash_rcor .bash_profile, when I start GNU screen, it doesn't recognize those changes. I can

.bash_rc或 中进行更改后.bash_profile,当我启动 GNU screen 时,它无法识别这些更改。我可以

source ~/.bash_profile

and it works for the current screen window I have open, but I have to do that for every screen window I have open.

它适用于我打开的当前屏幕窗口,但我必须为我打开的每个屏幕窗口执行此操作。

How do I get screen to read my latest changes in my bash configuration?

如何让屏幕读取我的 bash 配置中的最新更改?

采纳答案by gpojd

You have to do it in each screen that you have open since they are all different shells. If you need the change every time a new shell is opened, I suggest you put the changes in ~/.bashrc instead.

您必须在打开的每个屏幕中执行此操作,因为它们都是不同的外壳。如果每次打开新 shell 时都需要更改,我建议您将更改放在 ~/.bashrc 中。

Apparently, you can send a command to all windows at once using this syntax:

显然,您可以使用以下语法一次向所有窗口发送命令:

C-a :
at "#" stuff "source ~/.bash_profile^M"

回答by Mike

If you want screen to always treat your shell as a login shell, and source the same files that would be read if just started a new shell normally, add the following to ~/.screenrc(or maybe ~/.byobu/.screenrc, as pointed out in the comment):

如果您希望 screen 始终将您的 shell 视为登录 shell,并提供在正常启动新 shell 时将读取的相同文件,请将以下内容添加到~/.screenrc(或者~/.byobu/.screenrc,如评论中指出的那样):

shell -$SHELL

This way, you don't need to manually tell it to source your files each time you start a new screen. Though you would have to if you just made changes and wanted those changes to be reflected in your current screen.

这样,每次启动新屏幕时,您都无需手动告诉它获取文件的来源。尽管如果您只是进行了更改并希望这些更改反映在您当前的屏幕中,则您必须这样做。

The documentation for this (and lots of other screen details) can be found here. Basically, shellis a command to screen telling it to run the following when it needs to create a new shell. $SHELLis the usual variable holding the path to your preferred shell. And the dash -in front of $SHELLindicates that it should be run as a login shell (which will typically mean it sources your ~/.bash_profile, etc.).

可以在此处找到有关此的文档(以及许多其他屏幕详细信息)。基本上,shell是一个命令屏幕告诉它在需要创建新外壳时运行以下命令。 $SHELL是保存首选shell 路径的常用变量。-前面的破折号$SHELL表示它应该作为登录 shell 运行(这通常意味着它来自您的~/.bash_profile等)。

It's worth pointing out, however, that screen defaults to just inheriting most environment variables from the shell where you start screen; and a login sub-shell may alter some environment variables in unexpected ways. I ran into a situation where elements of my $PATH were basically permuted. I solved the problem thanks to this particularly excellent answeron superuser.

然而,值得指出的是, screen 默认只从你启动 screen 的 shell 继承大多数环境变量;并且登录子 shell 可能会以意想不到的方式改变一些环境变量。我遇到了一种情况,我的 $PATH 的元素基本上被置换了。由于超级用户的这个特别出色的答案,我解决了这个问题。

You may notice the sourcecommand available. It's important to note that this sources a file of screencommands, rather than shell commands. Other relevant (screen) commands include evaland exec.

您可能会注意到source可用的命令。重要的是要注意,这是screen命令文件的来源,而不是 shell 命令。其他相关(屏幕)命令包括evalexec