How can I tell whether I'm in a screen?
Solution 1:
Check $STY
. If it's null, you're on a "real" terminal. If it contains anything, it's the name of the screen you're in.
If you are not in screen:
eric@dev ~ $ echo $STY
eric@dev ~ $
If you are in screen:
eric@dev ~ $ echo $STY
2026.pts-0.ip-10-0-1-71
Solution 2:
Another way I've done it is to echo $TERM.
$ echo $TERM
screen
Since I end up doing this a lot, I added an alias into my .bashrc file:
alias trm='echo $TERM'
This way, whether in screen or not, if I just execute 'trm' it will show me whether I'm in SCREEN or elsewhere (usually XTERM).
Solution 3:
Alternative approach to check if you are in screen.
type:
Ctrl-a ?
If you see the screen help you are in screen.
Otherwise you'll get a question mark '?' on the prompt.
Solution 4:
Since all of the other methods here rely on environment variables (which can simply be overridden) or the command character for screen (which can also be overridden), the most foolproof way to check would be to list all the ancestors of the current process.
pstree --show-parents -p $$ | head -n 1 | sed 's/\(.*\)+.*/\1/' | grep screen | wc -l
If it prints 1, then the current process you're running has an ancestor with the word 'screen' in the executable's name, otherwise there wasn't.
A more facile visible inspection might be obtained from:
pstree --show-parents -p $$ | head -n 1 | sed 's/\(.*\)+.*/\1/' | less
Solution 5:
My solution to the problem is a lot simpler: just hitting TAB makes the full terminal blink (a quick video inversion) if you are inside GNU Screen.
Tested working on most Linux (Ubuntu, Kali, Debian, RaspBerry... etc) and FreeBSD, GUI and any terminal, local or remote, including CtrlAltFn ones.
As an exception for this method, please, note this (rather complex, but possible) case scenario:
- 1.- SSH into computer A (lets assume Linux).
- 2.- Enter a new
screen -S AScr
from remote terminal on computer A. - 3.- SSH from GNU Screen
AScr
terminal into Computer B. - 4.- Enter a new
screen -S BScr
from remote terminal on computer B.
You are inside a Screen
on cases 2 and 4, and outside a Screen
on cases 1 and 3, but the terminal will blink on cases 2, 3 and 4.