views:

50

answers:

4

I have a command I want to execute in .bashrc only when the current terminal window is managed by GNU screen. How do I tell it? 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

+2  A: 

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

mykhal
Screen seems to inherit $TERM value from the terminal session launching it. So in my case it is xterm-256color and it depends on the environment. What I want is the general solution that can work under all environment.
OTZ
@otz i was afraid of that :) updated another possibility
mykhal
The $TERMCAP approach applies to more cases, but misses cases where you SSH to other host under the same screen window, in which case $TERMCAP won't contain 'screen' substring. So, the applicability is exactly the same as $WINDOW, as it also loses its value once you ssh to other host. But all things equal, $WINDOW predicate works faster.
OTZ
Right. That's what I meant by "applicability is exactly the same". I've been looking at the source code of GNU screen, and it looks like there is no solution to it. One could write a function to do ssh and then set a special environment variable, but that's not really elegant.
OTZ
+1  A: 

Check variable $TERM

iniju
It may or may not work, depending on the environment.
OTZ
A: 

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

signine
A: 

I've looked at the actual source code of GNU screen. There is absolutely no way to do this for all cases.

OTZ