Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

is it somehow possible to check if I'm right now in screen session? I need it to determine if hitting ctrl+d would disconnect me from server or just close current screen. Thanks.

share|improve this question

6 Answers 6

up vote 26 down vote accepted

You can look at the $STY variable. If it is not "" then you are in a screen session. I am in screen

$ echo $STY 

I am not in screen

$ echo $STY

share|improve this answer
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
Works well, thanks! –  crodjer Aug 6 '14 at 9:34

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: http://stackoverflow.com/questions/3472287/how-do-you-tell-if-the-current-terminal-session-is-in-gnu-screen

share|improve this answer
This presumes that you are not deliberately messing with your TERM value for some reason. –  David Mackintosh Apr 11 '11 at 3:04

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.

share|improve this answer
Why are we not upvoting this. –  AndreKR Mar 6 '14 at 2:36

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

share|improve this answer

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!

share|improve this answer

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)
share|improve this answer
if there are screens attached somewhere else this might be an issue. –  Dennis Nolte Apr 29 at 9:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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