I use the screen program extensively. It has helped me tremendously by reducing the time spent in setting up session every day. However, now that I have >8 screens - I forget sometimes which screen I am in. Is there a way to find the name of the screen session that I am working in. screen -ls will list all the screen session.

Is there a way to detach from all the currently active screen sessions - e.g. at the end of the day.

  • 3
    I found that echo $STY will print the name of the current screen session. So the first question is answered. The second question i.e. how to detach from all currently active screen session in one go is still pending. I can think of writing a script to do this but that will mean parsing of the output of screen -ls command. Please suggest if there is a better way – doon Apr 7 '13 at 19:35
  • I'm confused. How can you have several screen sessions running on the same TTY? – tink Apr 7 '13 at 19:50
  • screen -ls gets its information from /var/run/screen/$USER (by default), so you can just check that. The filenames are STYs, and the execute bit indicates if the screen is attached – Michael Mrozek Apr 7 '13 at 21:44
  • You should set a session name and attach to a specific session – Ulrich Dangel Apr 7 '13 at 21:55

Once upon a time, I was using this simple shell script as a cron job, as you say, at the end of the day.


screen -ls \
  | awk '/\(Attached\)/{print $1}' \
  | while read line ; do
      screen -D $line ; 

Screen -D (power detach) also sends a HANGUP signal to the parent process of screen (usually closing the containing terminal). You can use -d (detach) if you prefer.

As @doon noticed, you can use echo $STY to know which screen are you in.


A slightly more reliable version than andcoz':

#! /bin/sh -
tab=$(printf '\t')
screen -ls |
  while IFS=$tab read -r session time state; do
    [ "$state" = "(Attached)" ] && screen -D "$session"

It still doesn't work if you use tab or newline characters in your session names.

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.