Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)?

share|improve this question
up vote 10 down vote accepted

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$ '
share|improve this answer
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 '11 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 '11 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 '15 at 12:44
    
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 '15 at 17:38

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)

notes:

  • 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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.