34

I run multiple screen sessions each created with 'screen -S name' and I would like to be able to display in the status bar the name I used to create the current screen session.

However, I cannot seem to accomplish this. Any ideas?

Neg_EV
  • 1,930
  • 1
  • 15
  • 23
  • 5
    See this [Super User question](http://superuser.com/questions/212392/how-to-include-screens-session-name-in-hardstatus). Also [here](http://old.nabble.com/Re:-Session-name-in-status-line-p22456825.html). – Dennis Williamson Jan 07 '11 at 03:29

8 Answers8

49

The easiest way to display the sessionname is

  C-a : 
  sessionname

(without specifying a name after sessionname)

See the "CUSTOMIZATION" section in man screen

nicola
  • 499
  • 1
  • 4
  • 2
  • 1
    Could you clarify this a bit? Is this a standalone command or parameters? – DBX12 Nov 14 '16 at 07:44
  • 3
    Type the following three keystrokes / commands one after the other: `Ctrl-A` then `:` then `sessionname` (Ctrl-A should be substituted with whatever you have set for the screen control keystroke, if you have changed it from the default of Ctrl-A.) You will then see output similar to: `This session is named '31438.Perlrocks'`. – Medlock Perlman May 03 '17 at 10:36
  • Neat! Is there a way to get this in a script running within `screen`? – jvriesem Mar 07 '19 at 21:39
24

screen has two status bars, the caption bar and the hardstatus bar, both of which use the string escapes specified in the "STRING ESCAPES" section of man screen. Unfortunately, there is no escape that directly refers to the session name.

However, there is a hack that will allow you to do this.

screen passes the session name to the shell using the $STY variable. When the shell attempt to set the window title (using one of these methods) screen captures that attempt, and stores it in something it confusingly calls "the window hardstatus," which does have an escape that you can use: %h.

So if you have either the caption or hardstatus bar set to include %h and have the shell attempt to set the window title to $STY, then the %h will be replaced with the session name when the bar is displayed.

rampion
  • 87,131
  • 49
  • 199
  • 315
  • Very close... Works great for the initial session however, once I am inside a screen session if I ssh to another host STY will no longer be set. Is there anyway to preserve that information or perhaps make screen only read the value once at the start of the session? – Neg_EV Apr 09 '10 at 15:02
  • 1
    @Neg_EV: Well, according to `man ssh`'s ENVIRONMENT section, if the sshd of the remote host allows it (default is not), you can set environment vars (like `STY`) in your local `.ssh/environment` file (so in your local `.bashrc`, just `cat STY=$STY > .ssh/environment`) and when you ssh to the remote machine that will set the appropriate environment vars. – rampion Apr 09 '10 at 15:47
  • @Neg_EV: If you don't want to (or can't) change the remote's `sshd` settings, then you could have your local `.bashrc` dump the STY value to a file, alias ssh to first scp that file to the remote host, and have your remote `.bashrc` source that file if STY is not set (that way the same .bashrc can be used remotely and locally). – rampion Apr 09 '10 at 15:49
  • I added `alias screen='screen -s -/bin/bash'` and `export PS1='$STY> '` in my .bash_profile, so wtih the _alias_, screen executes my .bash_profile in each session and with the _export_ I can see which session I'm working with. – i love mistaking Oct 02 '20 at 07:19
16

In current versions of screen, there is a flag %S for the hardstatus line.

MWE (.screenrc):

hardstatus on
hardstatus alwayslastline
hardstatus string "%S"

This displays the session name without the ID (like ${STY#*.}).

(Same answer to other questions here and here for completeness).

Community
  • 1
  • 1
Scz
  • 650
  • 7
  • 13
3

paraphrased from https://superuser.com/a/212520/151988, put this into your ~/.screenrc;

screen 
screen $SHELL -c 'screen -X caption always "$STY"'
Community
  • 1
  • 1
stratagem
  • 525
  • 6
  • 7
2

Super User has an answer to this that does not require $STY, instead using the backtick screen config command and screen -ls: https://superuser.com/a/212520

Community
  • 1
  • 1
wodow
  • 3,871
  • 5
  • 33
  • 45
1

If nothing else works (as for me), as a workaround you can create a window with number 0 and set title to your screen name:

screen -S myscreen
C^a :title "myscreen"
Dennis Golomazov
  • 16,269
  • 5
  • 73
  • 81
0

As max_cantor says in the SuperUser Answer, an escape character for the session name should be added to version 4.1.0. It looks like the escape character function was added with a relatively small patch back in 2008. So if you're feeling brave, you can git yourself a copy of the development version 4.1.0 and try it out.

I'll try this with the development version when I get a chance.

Community
  • 1
  • 1
Cory
  • 748
  • 7
  • 18
0

"screen -X sessionname" i'm using ssh,i thought this is a straight answer , sessionname will show at the screen bottom

Z W
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 17 '22 at 19:22