Is there a way to present the GNU Screen session name and window title in the prompt of the shell (let us say, the Bash prompt defined by PS1)?

4 Answers 4


Screen supplies some environment variables (from screen(1) manpage):

STY              Alternate socket name.
WINDOW           Window number of a window (at creation time).

The "at creation time" means that if you renumber a window (using screen's number command), the shell will not be told about the change and $WINDOW will still be the same as the first window number.

You could use something like:

PS1='\u@\h(${STY}:${WINDOW}):\w$ '
  • 1
    Perfect! This is what I was looking for. BTW, there is a way to get the window title, instead of the window number?
    – brandizzi
    Sep 19, 2011 at 1:13
  • Not that I know of, there is no api for screen, and when you issue commands there is no output - it goes to the current window in the screen. For example screen -xr 2466 -X windows.
    – Arcege
    Sep 19, 2011 at 1:26
  • Although it probably falls under the renumbering case, is there a way to access the window's name if it's set with Ctrl-a A ?
    – Thalis K.
    Jul 5, 2015 at 12:44
  • 1
    If GNU screen has been compiled with the -Q (query) option, then you can run some commands, such as title and windows and have the data display to stdout, however, without a trailing newline.
    – Arcege
    Jul 6, 2015 at 17:38

I always use precise screen session names. Then I can add screen's STY env var, with the numeric id stripped out (thanks dimo414), to PS1. I don't decorate it with curly braces or anything because I'm not always in a session. Simple example:

PS1='\u@\h ${STY#[0-9]*.} \w$ '

I use this:

(`echo ${STY} | sed -e 's/[0-9]*\.//g'`:${WINDOW}:`screen -Q title`)

removes the process number from ${STY}
includes the window number (as mentioned "at creation time")
includes the window title (as returned from screen -Q title)


  • first time it runs, screen -Q waits for a return keystroke
  • i set the window title with C-a C-A and then source my .profile to update the prompt
  • 6
    Using ${STY#[0-9]*.} avoids needing to pipe through sed.
    – dimo414
    Jan 10, 2017 at 17:21

I generally use named screens, e.g.: screen -S vim ## see: screen -h

To get a PS1 prompt in the screen that shows the name of the screen session (to differentiate screen sessions for the normal terminal sessions) I needed a two-part approach.

  1. Set the prompt in a ~/.screenrc file.
  2. Source that file (within the screen session) via a normal BASH ~/.bashrc alias.
# -----------------------------------------------------------------------------
# ~/.screenrc    ## or whatever you want to name it

PS1='[\u@\h (screen: ${STY#[0-9]*.}) \w]$ '
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# ~/.bashrc

alias ssrc='source ~/.screenrc'    ## or whatever you named it (comment above)
# -----------------------------------------------------------------------------


[me@vps1360 ~]$ screen -S test

  # -----------------------------------------------------------------------------
  # Named screen session:

  [me@vps1360 ~]$ ssrc
  [me@vps1360 (screen: test) ~]$    ## PS1 prompt with screen session name
  # Ctrl-a d
  [detached from 9278.test]
  # -----------------------------------------------------------------------------

[me@vps1360 ~]$
  • You could probably get away with using something like this in your bashrc file and forget about the alias and the screenrc file: PS1='[\u@\h ${STY+(screen: ${STY#[0-9]*.}) }\w]$ '. It adds the parenthesis in the prompt if STY is set. Also, you never have to export PS1.
    – Kusalananda
    Aug 3, 2022 at 19:37
  • I didn't know about screen -S. Thanks!
    – brandizzi
    Aug 3, 2022 at 19:39
  • I tried various combinations of sourcing from ~.bashrc (alias screen='screen -c <pathToFile> ..., ~/.screenrc, /etc/screenrc/. Part of the issue, I believe, is that screen sessions are instances of an existing (single) screen process? Anyway, ... in the end the approach above was the simplest, facile solution. ;-) Aug 3, 2022 at 20:42

You must log in to answer this question.

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