I need it to determine if hitting ctrl+d would disconnect me from server or just close current screen.

Is it somehow possible to check if I'm right now in screen session?


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 

I am not in screen

$ echo $STY

  • 13
    This presumes that you are still within a running session on the local computer. If you start up screen and then SSH somewhere else, this won't work. – David Mackintosh Apr 11 '11 at 3:05
  • 2
    if test -n "$STY"; then printf "This is a screen session named '$STY'.\n"; else printf "This is NOT a screen session.\n"; fi – aggregate1166877 Sep 19 '15 at 17:08
  • 3
    @DavidMackintosh if you're SSH'd into somewhere else, hitting ctrl-D will "disconnect me from server", which is exactly what the question asks about. – womble Aug 11 '16 at 8:24
  • 1
    @aggregate1166877 I have this if you posted as an alias in every single machine that I use now. – Eduardo Bezerra Sep 10 '16 at 12:32

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

Ctrl-a -d (to exit screen)

root@deore:/volumes# echo $TERM

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

  • 4
    This presumes that you are not deliberately messing with your TERM value for some reason. – David Mackintosh Apr 11 '11 at 3:04
  • 1
    If you're changing your TERM variable while inside screen, you're inviting all sorts of trouble on yourself. – womble Aug 11 '16 at 8:24
  • Well my Solaris 2.6 nodes don't play nice with TERM values set to screen values. – David Mackintosh Aug 11 '16 at 17:56
  • 3
    "$TERM" = "screen" seems to be preserved when entering sudo environment, unlike the $STY option. – Melebius Dec 13 '16 at 10:00

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.

  • 4
    Why are we not upvoting this. – AndreKR Mar 6 '14 at 2:36
  • 1
    How is this the best solution? What if you're not in screen, and ^A^T is the key sequence for "nuke your homedir" in the program you're currently running? – womble Aug 11 '16 at 8:25
  • 2
    @womble If you are in an unknown program that may do random destructive things as a response to key sequences and commands, then nothing is safe. – Gurgeh Aug 12 '16 at 8:55
  • 2
    @AndreKR: It doesn't work in a script – Frank Meulenaar Jan 19 '19 at 20:38
  • 1
    im not upvoting because in in a screen, and screening into another screen, and i literally need to see if $STY is even set. which for some reason its coming back empty. – Brian Thomas Apr 23 '19 at 1:18

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!

  • 1
    This is perfect! It does not get in the way (bottom position), it is always visible (and colored) and provides useful info about system. I posted this here also stackoverflow.com/a/43571028/2450431 – hrvoj3e Apr 23 '17 at 12:22

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

  • I think I know what you're suggesting, and it could in some situations avoid this problem entirely. It might be more helpful if you describe what you mean by showing (e.g.) an example .screenrc file. – jvriesem Apr 29 '19 at 18:42

If you are looking at a command line prompt, you can just type something, anything, and hit Ctrl+A. If your cursor jumps to the beginning of the prompt, you're not inside a screen. If you additionally have to hit A, then you are.


Do a screen -ls. It's going to explicitly indicate Attached versus Detached status.

Example attached:

$ screen -ls | grep tached
3132.pts-0.esavo00      (Attached)

Example detached:

$ screen -ls |grep tached
3132.pts-0.esavo00  (Detached)
  • 7
    if there are screens attached somewhere else this might be an issue. – Dennis Nolte Apr 29 '15 at 9:15
screen -ls

to view your sessions and

screen -r sessioninfo

to reconnect to a disconnected one, if detached.

screen -D -r sessioninfo

to reconnect to a disconnected one.

  • 1
    This doesn't answer the question. – womble Aug 11 '16 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.