140

Is there a way to give a particular name to a unix screen session? For instance, say I'm running the same program multiple times, each with different parameters and I want to tell which one is which.

0

6 Answers 6

178

You can name a session when starting it with the -S name option. From within a running screen, you can change it by typing

Ctrl+A,: followed by sessionname name(1).

You can view running screen sessions with screen -ls, and connect to one by name with

screen -xS name

(1):name is and an arbitrary string which will become the new session name. If the session name contains whitespace, quote it with single or double quotes.

Within a single screen session, you can also name each window. Do this by typing Ctrl+A, A then the name you want. You can view an interactive list of named windows by typing Ctrl+A, ", and select the one you want to switch to from that list.

Naming both screens and terminals within screens is really helpful for remembering what they are and why you started them in the first place.

2
  • 9
    Renaming a screen session after it's started is possible (using :sessionname NEWNAME), but it's "generally discouraged". The $STY environment variable still refers to the original name. See the screen manual: gnu.org/software/screen/manual/html_node/Session-Name.html Jun 14, 2016 at 20:02
  • heads-up: you need a uppercase A, to rename the window!
    – oceanBT
    Jan 28, 2022 at 12:57
39

There are two concepts here, and I'm not sure which one you have in mind:

  • You can have multiple screen windows. Each window runs a shell or other program. All the windows are hosted by the same process. C-a c creates a window, C-a n and C-a p switch to the next/previous window, and so on.
  • You can have multiple screen sessions. Each session is hosted by its own process and is independent of all other sessions. Starting screen without any reattach (-r or -R) option creates a new session.

Windows have titles, which can be set through the -t command line option, the C-a A key binding, the title command, or the \ek escape sequence. See shellter's answer for more details.

Sessions can have names. You'd typically set the name on the command line with the -S option; if you don't specify a name, screen makes one up. If you use multiple screen sessions, you'd typically give them different session names. You can list the running screen session with screen -ls; the first word on each session line is 12345.sessionname where 12345 is the screen process ID. Use screen -r sessionname or screen -r 12345 to resume a session indicated through its name or process ID.

2
  • 1
    In the default key bindings, to change title you have to use C-a A.
    – enzotib
    Dec 21, 2011 at 6:49
  • I think this should be the accepted answer as it states the difference between session and window very clearly.
    – rioted
    Aug 26, 2022 at 13:12
20

To start:

screen -S mysession

To resume:

screen -r mysession

Credit

1
  • 2
    This is the simpler and straightforward answer.
    – vpz
    Nov 17, 2021 at 13:28
19

Ah... Screen, it takes me back ;-)

For one window

-t name
    sets the title (a.k.a.) for the default shell or specified program. 
    See also the "shelltitle" .screenrc command.

For multiple sessions started from your .screenrc

screen -t top 2 nice top
screen -t ....

Here's a link to one on-line copy of the man-page for screen.

As @MrFooz rightly points out, the $0 is not expanded when inside single quotes, the correct code is,

cat scrnTitle.sh
#/bin/bash
echo -ne "\ek${0}\e\\"

As pointed out in comment below by @lindes, there is also a keyboard/interactive way to change the title: Ctrl aA (Control-a followed by capital A).


If you're going to be using screen, it is really worth reading through the man page completely, as there are lots of features.

I still don't have a system with a working screen so I can't test to verify.

13
  • Thanks, this helps a lot. Is there anyway to rename an already existent screen session? Apr 27, 2011 at 21:19
  • @Charlotte : see edits. Thanks for the votes!
    – shellter
    Apr 27, 2011 at 21:44
  • Nice answer @shellter! I'm curious, did you say "takes me back" to mean you use something else these days (e.g tmux), or just that it's over 32 years old?
    – tutuDajuju
    May 17, 2019 at 7:27
  • 1
    @tutuDajuju : Gulp, now that I think about it, I've used screen at least 25 years. Takes me back, because now it is a special use case that would take me to screen. Normally I just keep opening X-Terms, but I remember when it seemed impossible to run X on a 486 PC ;-) Cheers!
    – shellter
    May 17, 2019 at 13:49
  • 2
    Nit: double quotes are needed instead of single quotes so that the ${0} substitution occurs: echo -ne "\ek${0}\e\\".
    – Mr Fooz
    Mar 2, 2021 at 14:25
6

While running screen:

Ctrl+a (or whatever your screen escape sequence is) A

5

The answers above already tell you how to name a screen when you start it. They also point out that a screen can't be renamed after it has been started. The window title can be set but the name used to attach to the screen remains pid.pty.host.

However, to achieve a useful effect I've found that using alias work pretty well. If I forget to name a screen or find myself in a session with a bunch of screens up that have naturally become screens for particular tasks I simply set an alias for the command to attach to them.

example:

alias goncompile='screen -r 2354'

Issuing the alias command by itself will remind you what screens you have up and command you have set to attach to them.

Use unalias to remove them.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .