Is it possible to obtain the current name of the xterm window?

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'

This will set the title to my title:

echo -ne '\e]0;my title\007'

And this will restore the prior window and icon titles:

echo -ne '\e[23t'

Use xtitle script.

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

http://www.shelldorado.com/scripts/cmds/xtitle


For the trivial, (and probably most common case), where one might wish to query the prompt for the current xterm, simply use xprop -id $WINDOWID WM_NAME. If you want to extract the value for manipulation in a shell script, you can do something a bit more expensive such as:

   curtitle=`xprop -id $WINDOWID WM_NAME|awk '{print $3}'|xargs echo`

Using bash alone (to avoid the invocation of awk and xargs):

   curtitle=`xprop -id $WINDOWID WM_NAME`
   curtitle=`eval echo ${x##*=}`

The xargs echo in the first example and the eval in the second example is simply there to strip off the quotes that xprop puts around the value.

Replace WM_NAME with WM_ICON if you want to obtain the icon name rather than the string in the title-bar.

Tags:

Xterm