↑ arvau

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

I have a command I want to execute in .bashrc only 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


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

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

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

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

UPDATE

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

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

also not 100% guaranteed

UPDATE2

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

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

Check variable $TERM

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

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

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

signine is correct

See the doc

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

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

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

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

Just add/edit a file ~/.screenrc in your $HOME folder and add caption command:

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 .screenrc you 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:14 Screen 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 I found this hit here on SO.