Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I am trying to get screen to set my xterm title. I have this working outside of screen, but screen keeps whatever title was in place when I started it. Here is my .bashrc:

function bash_prompt_command() {
    # How many characters of the $PWD should be kept
    local pwdmaxlen=25
    # Indicate that there has been dir truncation
    local trunc_symbol=".."
    local dir=${PWD##*/}
    pwdmaxlen=$(( ( pwdmaxlen < ${#dir} ) ? ${#dir} : pwdmaxlen ))
    local pwdoffset=$(( ${#NEW_PWD} - pwdmaxlen ))
    if [ ${pwdoffset} -gt "0" ]

    export NEW_PWD

# Color chart @ http://wiki.archlinux.org/index.php/Color_Bash_Prompt
case "${TERM}" in
        TITLEBAR='\[\033]0;\u@\h > ${NEW_PWD}\007\]'
        PS1="${TITLEBAR}\[\e[1;32m\][\e[0;36m\]\u\e[1;32m\]@\e[1;33m\]\h\e[1;32m\]] \e[0;37m\]\${NEW_PWD}/ \e[1;32m\]\$ \[\e[0m"
        TITLEBAR='\[\033]0;\u@\h > ${NEW_PWD}\007\]'
        PS1="${TITLEBAR}\[\e[1;32m\][\e[0;36m\]\u\e[1;32m\]] \e[0;37m\]\${NEW_PWD}/ \e[1;32m\]\$ ${ESC}\[\e[0m"
        PS1="\[\e[1;32m\][\e[0;36m\]\u\e[1;32m\]@\e[1;33m\]\h\e[1;32m\]] \e[0;37m\]\${NEW_PWD}/ \e[1;32m\]\$ \[\e[0m"

And here is my .screenrc:

hardstatus alwayslastline
hardstatus string '%{= kg}[%{Y}%H%{g}][%= %{= kw}%?%-Lw%?%{=b kR}(%{W}%n-%t%?(%u)%?%{=b kR})%{= kw}%?%+Lw%?%?%= %{g}][%{Y}%l%{g}]%{g}[%{B}%m.%d.%Y %{G}%c%{g}]'

termcapinfo xterm|xterms|xs|rxvt ti@:te@
termcapinfo xterm 'hs:ts=\E]2;:fs=\007:ds=\E]2;screen\007'

altscreen on
shelltitle '$ |bash'

What am I doing incorrectly?

Update (19 August 2010): The problem is that you cannot update the terminal's title from within screen when you set alwayslastline. So my solution was to just give up and settle for a predetermined, useful, title for my screen sessions. My updated .bashrc and .screenrc can be found at http://bitbucket.org/jsumners/rcfiles/src.

share|improve this question

3 Answers 3

up vote 4 down vote accepted

The escape codes are different inside screen.

This outside of screen:

PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'

is equivalent to this inside screen:

PROMPT_COMMAND='echo -ne "\033_${USER}@${HOSTNAME}: ${PWD}\033\\"'

Changing your case ${TERM}="screen" titlebar to

TITLEBAR='\[\033_\u@\h > ${NEW_PWD}\033\\\]'

will solve your problem.

share|improve this answer
This is not working. I don't understand why. –  jsumners Jul 6 '09 at 15:06
I have determined that setting hardstatus alwayslastline as I do for screen prevents screen from setting the xterm title. Now if I can just figure out how to do both at the same time. –  jsumners Jul 6 '09 at 19:47
Is it because your hardstatus has %t (for title) in it? –  crb Jul 6 '09 at 20:10
Nope. Just removed that and get the same result. I believe I am going to have to set an alias for screen that sets the window title and then launches the program. And then just give up on dynamic terminal titles while using screen. :( –  jsumners Jul 7 '09 at 12:17
Or get rid of hardstatus/report a bug in screen... –  crb Jul 7 '09 at 13:34

Actually, there is a way to send escape sequences directly to the xterm, passing through screen. It was not easy to find, but from the screen code (src/ansi.c) I think its been there since 2005. I have it working in xterm, Gnome Terminal, and putty. I found a note in mintty source indicating it does not work there simply because they don't handle DCS (unless it was fixed recently).

From the documentation for screen below (at http://www.gnu.org/software/screen/manual/html_node/Control-Sequences.html )

ESC P (A) Device Control String
          Outputs a string directly to the host terminal without interpretation.

The ANSI DCS (Device Control String) is an escape code that is used to send directly to a terminal (I think that was its original purpose from many years ago). Such a string is terminated with an ST escape code (String Terminator).

Wrap the string to set the terminal's window title inside a DCS..ST block, and it goes through screen and updates correctly, even with hardstatus alwayslastline.

DCS = \033P, ST = \033\

Example - to update the window title with the current working directory, use

echo -ne "\033P${NEW_TITLE}\033\\"

Better late than never! Hope this helps someone.

Just in case I'm wrong about the screen source changes: I am running the latest from screen-session git, which in turn is using almost the latest from screen git (with some changes specific to screen-session). But the log messages are dated from 2005 screen (you can see the changes made to that commit regarding DCS handling if you clone the screen git repo and use the command below).

git clone git://git.savannah.gnu.org/screen.git
git difftool e6618a14^! src/ansi.c
share|improve this answer
This works as advertised. Thanks a lot. –  AnkurVj Mar 24 '14 at 5:56

crb is kind of right, but

a) the escape code he uses do not work for me. It should be \033k instead of \033_. See screen (1) under "TITLES (naming windows)". So it should be:

PROMPT_COMMAND='echo -ne "\033k${USER}@${HOSTNAME}: ${PWD}\033\\"'

b) this kind of escaping is only used for setting the title of the screen-window. One instance of screen can have multiple windows, each of them has a name, that is what is displayed in hardstatus and caption with the t escape sequence.

c) the normal bash escaping works if you set the right terminfo for xterm in .screenrc:

termcapinfo xterm 'hs:ts=\E]2;:fs=\007:ds=\E]2;screen\007'

(this also works on my machine for putty)

So in order to set the right title for both the screen window and the putty/xterm title, I use someting like:

PROMPT_COMMAND='echo -ne "\033]0;$XTERM_TITLE\007\033k$SCREEN_TITLE\033\\"'

for reference: http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x395.html

share|improve this answer

Your Answer


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.