Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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
up vote 5 down vote accepted

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.

share|improve this answer

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.

share|improve this answer

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.