bash 您如何判断当前终端会话是否在 GNU 屏幕中?

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

How do you tell if the current terminal session is in GNU screen?

bashterminalgnugnu-screen

提问by OTZ

I have a command I want to execute in .bashrconly when the current terminal window is managed by GNU screen. How do I do this? Is there an environment variable for it? I used to have

我有一个命令,我只想.bashrc在当前终端窗口由 GNU screen 管理时执行。我该怎么做呢?有它的环境变量吗?我曾经有过

if [ -n "$WINDOW" ]; then
    command
fi

But from what I can tell, $WINDOWmay or may not be defined across all screen managed sessions.

但据我所知,$WINDOW可能会或可能不会在所有屏幕管理会话中定义。

回答by signine

Check for the environment variable $STY (contains info about the screen) or for $TERM being 'screen' (probably less reliable).

检查环境变量 $STY(包含有关屏幕的信息)或 $TERM 是否为“屏幕”(可能不太可靠)。

回答by rhlee

signine is correct

签名是正确的

See the doc

查看文档

http://www.gnu.org/software/screen/manual/html_node/Environment.html

http://www.gnu.org/software/screen/manual/html_node/Environment.html

STY

Alternate socket name. If screenis invoked, and the environment variable STYis set, then it creates only a window in the running screensession rather than starting a new session.

STY

备用套接字名称。如果screen调用并STY设置了环境变量,那么它只会在正在运行的screen会话中创建一个窗口,而不是启动一个新会话。

回答by mykhal

check $TERM, it is set to 'screen' in screen session.. (but not 100% guaranteed)

检查$TERM,它screen在屏幕会话中设置为“ ”..(但不是 100% 保证)

UPDATE

更新

alternatively, you can utilize the fact that in screen, $TERMCAPcontains screen substring:

或者,您可以利用在屏幕中$TERMCAP包含屏幕子字符串的事实:

[[ $TERMCAP =~ screen ]] && echo "in screen"

also not 100% guaranteed

也不是 100% 保证

UPDATE2

更新2

if neither approach works, you can find some inspiration in screen manual

如果两种方法都不起作用,您可以在屏幕手册中找到一些灵感

回答by hrvoj3e

I would like to suggest an alternative solution that is always visible and does not require checking to know you are inside a screensession.

我想建议一个始终可见的替代解决方案,不需要检查就知道您在screen会话中。

Just add/edit a file ~/.screenrcin your $HOMEfolder and add captioncommand:

只需~/.screenrc在您的$HOME文件夹中添加/编辑一个文件并添加caption命令:

caption always "%{= kc}Screen session on %H (system load: %l)%-28=%{= .m} %Y-%m-%d %0c"

and you will get at the very bottom one line that is always visible and colored.

你会在最底部看到一条始终可见且有颜色的线。

Screen session on host (system load: 1.50 1.40 1.30)        2017-04-23 14:06

Please note:

请注意:

When you are inside screen on host1 and open another SSH session to host2 and open screen there which also has .screenrcyou will get two lines at the bottom stacked from bottom up (like a wrapper).

This is VERY useful in that case!

Screen session on host2 (system load: 0.01 0.03 0.05) 2017-04-23 14:14Screen session on host1 (system load: 0.00 0.00 0.00) 2017-04-23 14:14

当您在 host1 上的 screen 内部并打开另一个到 host2 的 SSH 会话并在那里打开 screen 时,.screenrc您将在底部看到两行自下而上堆叠的行(如包装器)。

这在那种情况下非常有用!

Screen session on host2 (system load: 0.01 0.03 0.05) 2017-04-23 14:14Screen session on host1 (system load: 0.00 0.00 0.00) 2017-04-23 14:14



Additional info:

附加信息:

https://www.gnu.org/software/screen/manual/html_node/Caption.html

Command: caption always|splitonly [string]

https://www.gnu.org/software/screen/manual/html_node/String-Escapes.html#String-Escapes

https://www.gnu.org/software/screen/manual/html_node/Caption.html

Command: caption always|splitonly [string]

https://www.gnu.org/software/screen/manual/html_node/String-Escapes.html#String-Escapes



I found this hit hereon SO.

我发现这个打这里的SO。

回答by iniju

Check variable $TERM

检查变量 $TERM