Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This LDP Howto demonstrates how to change the title of an xterm.

I know that this is also possible using the xtitle command.

Before invoking a long-running action, my script uses such techniques to change the title of its xterm window.

I would like to extract the name of the xterm window prior to changing it so that, once the long-running action is completed, I can restore the title to its previous value.

It would sort of be an inverse to xtitle... Is this even possible?

share|improve this question
Related stackoverflow question. It looks like xterm has a stack for window title, if you can figure out how to use it. –  jw013 Jan 7 '12 at 3:36
I'll take a look. Thanks for the tip! –  TheGeeko61 Jan 7 '12 at 6:15

3 Answers 3

up vote 4 down vote accepted

Use xtitle script.

There are many variants on internet, however, I tend to use one which depends on xprop


share|improve this answer
Excellent! This is EXACTLY what I was hoping for... and it not only works for xterm but (unlike other solutions) also works for gnome-terminal. Thank YOU! –  TheGeeko61 Jan 7 '12 at 19:02
Do note though that obtaining the title via the X protocol, which is what xtitle does, only works for local sessions. –  ak2 Jan 7 '12 at 20:07

If I understand your question right, you should be able to get the current xterm title by inspecting the $PROMPT_COMMAND variable, e.g:

printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"

and you can set it like (linebreak for readability):

0;%s@%s%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
share|improve this answer
The PROMPT_COMMAND is not the most authoritative way to get the title since it is not always set, and any command you run could change the title between prompts. For the most accuracy, terminal emulator specific control sequences are probably the way to go. –  jw013 Jan 7 '12 at 17:39
This would not work for cases where other apps are setting the title. –  TheGeeko61 Jan 7 '12 at 18:55

There is a control sequence for querying the current title, but it's usually disabled for security reasons. That's because it allows a malicious program or even just a catted text file to insert arbitrary character sequences as if typed on the keyboard, by first setting the title and then asking to have it reported back.

Hence the title stack was introduced instead. The following command will save the current window and icon titles onto the stack:

echo -ne '\e[22t'

And this one will restore them:

echo -ne '\e[23t'
share|improve this answer
Thank you for the details. I appreciate it. –  TheGeeko61 Jan 7 '12 at 18:19

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.