How do I know I'm running inside a linux "screen" or not?
Solution 1:
(Stolen from "How can I tell whether I'm in a screen?" over on StackOverflow and authored by user jho. P.S. You can't vote for a duplicate across StackExchange sites.)
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
If you use tmux instead of screen, also check $TMUX
. To add this to your prompt, add the following to your ~/.bashrc
:
if [ -n "$STY" ]; then export PS1="(screen) $PS1"; fi
if [ -n "$TMUX" ]; then export PS1="(tmux) $PS1"; fi
Solution 2:
Look for $STY
which provides details that screen
uses to communicate with itself; $WINDOW
will then be the current screen
window number.
Solution 3:
The simple check I usually use is to just hit Ctrl-a:
If the cursor jumps to the start of the line, I'm not inside a screen session.
If nothing happens, I know that I'm inside a screen session and that I've just used the screen control key. I then hit a (jump to the beginning of the line), w (show current screen windows) or execute some other "harmless" screen command to get back to the command prompt.
(Of course, this only works if you are currently executing bash or some other piece of software that "jumps to the start of the line" or does something equivalently harmless when hitting Ctrl-a.)
Solution 4:
Well, most of the time (absent someone's attempts to screw with things) your TERM
will be set to screen
(or at least mention screen
somewhere).
The easy solution to the problem is just run screen everywhere. Don't leave home without it, I say.
Solution 5:
I keep this in my .bashrc
:
PS1='[\u@\h \W'
if [ "$WINDOW" ]; then PS1="$PS1 ($WINDOW)"; fi
PS1="$PS1]$"
It's not foolproof, but whenever I create a new screen, it puts the window number in the prompt.
If I'm not running screen
, there's no number.