143

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.

Braiam
  • 35,991
well actually
  • 3,225
  • 5
  • 22
  • 11

6 Answers6

181

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.

AdminBee
  • 22,803
Lars Yencken
  • 1,911
  • 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: https://www.gnu.org/software/screen/manual/html_node/Session-Name.html – Keith Thompson Jun 14 '16 at 20:02
  • heads-up: you need a uppercase A, to rename the window! – oceanBT Jan 28 '22 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.

22

To start:

screen -S mysession

To resume:

screen -r mysession

Credit

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.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
shellter
  • 704
  • Thanks, this helps a lot. Is there anyway to rename an already existent screen session? – well actually Apr 27 '11 at 21:19
  • @Charlotte : see edits. Thanks for the votes! – shellter Apr 27 '11 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 '19 at 07: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 '19 at 13:49
  • @shellter FYI, I ran X on a 486 PC with 8Mb RAM back in 1992... – Eno Oct 16 '19 at 16:01
  • @Eno : Oh well, I recall 1MB 486 PC being a big thing! ;-/ Glad someone got it to work ;-) – shellter Oct 16 '19 at 17:08
  • @shellter I used the first Linux distribution - SLS - which came on 30 floppies :-) I setup a dual-boot with an 80Mb partition for Linux (including 10Mb swap :-) – Eno Oct 17 '19 at 21:46
  • That's some heavy lifting. I didn't really get to use an *nix until my job where I started on Sun 3 hardware. Wow, 30 floppies, that takes some determination. I thought Windows on 8 floppies was bad ;-! Ciao for niao! – shellter Oct 17 '19 at 22:13
  • 2
    Nit: double quotes are needed instead of single quotes so that the ${0} substitution occurs: echo -ne "\ek${0}\e\\". – Mr Fooz Mar 02 '21 at 14:25
  • Yeah, very good. Updated and thanks! – shellter Mar 03 '21 at 00:20
  • Ooh, the "\ek[...]\e\" thing is new to me; cool! That said, two suggestions to make this answer better (though it still has my upvote): (1) Add a mention of C-aA as an interactive way to do the same thing; (2) when editing your answer, just edit it to correct it, removing the bad information, instead of leaving bad information in and adding good information after it. – lindes Jan 12 '22 at 18:32
  • @lindes : Hmm, your comment just showed up in my in-box (today-ish). Yes, you're right on both accounts, I will update my answer with your improvments. Thanks. I used the <kbd>C-a often as a user, but I don't think I ever realized/needed to change the title. Good to know that is possible! Cheers! – shellter Mar 22 '22 at 17:43
  • @shellter: cool. And given the round trip delays in comments, I figured I'd go ahead and just do an edit to help things be even better. It needs to be approved first, but hopefully you'll see it. In the mean time, typing <kbd>C-a</kbd> (without any backticks, though I'm typing it within them in this comment), should get you what you want (and is what I did in my edit). If I remember correctly, it doesn't work in comments? Here's attempting it anyway: C-a – lindes Mar 28 '22 at 18:57
6

While running screen:

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

ninjalj
  • 1,427
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.

Nidal
  • 8,956