How to check if I'm in screen session?

Solution 1:

You can look at the $STY variable (a variable set by the screen command). If it is not "" then you are in a screen session.

I am in screen

$ echo $STY 
29624.pts-1.iain-10-04
$

I am not in screen

$ echo $STY

$

Solution 2:

You can look at the $TERM variable.

echo $TERM

If it's a screen session, the term variable should return "screen".

root@deore:/volumes# echo $TERM
screen

Ctrl-a -d (to exit screen)

root@deore:/volumes# echo $TERM
xterm

Also check: https://stackoverflow.com/questions/3472287/how-do-you-tell-if-the-current-terminal-session-is-in-gnu-screen


Solution 3:

Unless you have changed the default key bindings, you can do Ctrl+a -> Ctrl+t, which will show the time, if you are in screen. This will work even if you have ssh:d away somewhere else, unlike the other suggestions.


Solution 4:

The caption command in the ~/.screenrc is a nice way to differentiate a screen session.

I'm personally using this:

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

It adds a line like this one at the bottom of the screen:

Screen session on gbook (system load: 1,75 1,74 1,68)                   Lun 05.01.2015 13:01

With the first part (system name + load) in green and the date in pink. Useful and hard to miss!


Solution 5:

I have found another solution:
Modify your .screenrc, so my screen session looks completely different from normal terminal.