Sign up ×
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 @
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

share|improve this question

5 Answers 5

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. – James Sumners 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. – James Sumners 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. :( – James Sumners 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 )

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 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:

share|improve this answer

I'm not going to answer the question completely, but let me suggest the way of solving the issue. Many window managers support EWMH specification. There is also a command line tool wmctrl which is able to interact with them. Using this tool, one may easily change active window's title with the following command:

  wmctrl -r :ACTIVE: -T "FooTitle"

It is also possible to change window icon, layout and other properties.The tool probably will not work for PuTTY since Windows doesn't support EWMH, but most of the Linux systems should accept it.

With this method it doesn't matter whether you use screen or not.

share|improve this answer

Last 2 weeks I have searched many forums and I haven't found the answer for the question:

How to get all command output to Putty title?

Needed it for other programs to know when some jobs on a server is done and is it done right or wrong. Plink stdout and stdin wasn't working, I used many tweaks with wait delays and for some commands that worked for others not. XSEL and XCLIP couldn't be installed on that server.

So here is the solution:

  1. Get command output in a file.
  2. Echo that file to title.

On putty client and suse server it looks like this:

ls /home | grep domagaja > logutoit.txt 
echo -e "\033]0\\;$(cat logutoit.txt)\\007\\c"

Hopefully someone will be able to use this. It won't work for all server types and putty client settings, of course, but the idea should work well.

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.