I created a screen session using the command:

screen -t name

Based off this name, is there any way to reopen this session (assuming there are no duplicate names) ?


I don't think there is any option for that -- other than looking through the process list:

screen -r $(pgrep -f '\<SCREEN.* -t name\>')

On *BSD something different is needed:

screen -r $(pgrep -t- -f '\<screen.* -t name\>')

The -t- option of pgrep directs it to match only processes with no controlling terminal, in this case only the screen backend process, not the first attached display (screen does change the argv[0] of the backend process to "SCREEN" everywhere, but on *BSD, changes to the argv strings are not mirrored in the process list, the way they are on Linux).

On FreeBSD the -a option ('also match ancestors of the calling process') may be also needed. Unfortunately, pgrep -t- is not supported on Linux, and pgrep -a does something completely different.

So, putting it all in a function:

# usage tscreen title [args ...]
    title=$1; shift
    screen -r $(
      pgrep -f "\\<SCREEN.* -t $title\\>" ||
      pgrep -t- -f "\\<screen.* -t $title\\>" ||
      pgrep -at- -if "\\<screen.* -t $title\\>" ||
      echo "title=$title"
    ) "$@"
  • can you break down the script to explain it. Where does the name go? – William Sep 24 '18 at 22:08
  • The name is name -- pgrep looks through the process list for a process with argv like ^SCREEN.* -t name$, finds a pid, then it looks through the screen sockets inside ~/.screen (which are of the form pid.tty.host) for one socket that matches, whose basename in turn could be used as an argument to the -r option. – mosvy Sep 24 '18 at 22:15
  • turns out that the -r accepts just a pid instead of pid.tty.host; please confirm if that works for you, too. – mosvy Sep 24 '18 at 22:33
  • I can't seem to get this to work on macOS. Not sure if it needs a different approach or not. – William Oct 15 '18 at 18:28
  • 1
    pgrep -f 'test1' will match too much (any program which contains test1 in one of its arguments). But if you can start screen yourself, and it's not started with -t name from a script you cannot modify, start it with the -S name -t name options instead and then you could attach to it by just screen -r name or screen -x name. – mosvy Nov 4 '18 at 22:44

All you need to do is to use the session name as an argument on the command line:


If you have more than one screen with the same session name, you'll also need to specify the PID, like so:

[jenny@sameen ~]$ screen -r test1
There are several suitable screens on:
    23669.test1 (Detached)
    23594.test1 (Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.
[jenny@sameen ~]$ screen -r 23669.test1

This is verified in screen 4.01.00devel on RHEL7 and 4.04.00 on BSD.

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.