Escape Sequences to set Terminal Title Broken in OpenSUSE 13.1?

Using bash in OpenSuSE 13.1, I set the following environment variable in .bashrc and rerun bash:

export PROMPT_COMMAND=‘echo -ne “\033]0;${USER}@@${HOSTNAME}: ${PWD}\007”’

but the xterm title doesn’t reflect my custom text. (I’ve used two @'s to ensure I’m seeing my change). This also happens with konsole if go to Settings > Edit Current Profile > Tabs and set “Table title format” to %w (window title set by shell). According to many searches, the above sequence should work just fine.

If I unset that environment variable and directly echo the same string, no change - it still doesn’t work. Thus it doesn’t seem to be related to bash configuration (dot files).

Anyone have any ideas what’s wrong? Is this crucial feature broken in KDE 4.11.5?

Hi,

Im not sure what are you trying to do with that prompt command. There are characters/strings in there that is special to the shell so it will not work, you need to escape it. Still i don’t understand what you want to achieve with this. Maybe you want PS1 instead?

First, this sets the prompt (actually it causes the shell to run a command before printing the prompt), not the window title.

Second, escape sequences work fine, but yours are wrong and make no sense.
Try this e.g. to see that it works in principle:

export PROMPT_COMMAND='echo -ne "\0333;12r\0333H${USER}@@${HOSTNAME}: ${PWD}> "'

Btw, you don’t need to rerun bash after setting the variable…

And you can set the prompt by setting the variable PS1, no need to run echo…

PS1="\0333;12r\0333H${USER}@@${HOSTNAME}: ${PWD}> "

See e.g. here for more escape sequences: http://misc.flogisoft.com/bash/tip_colors_and_formatting

Nope, PS1 is specifically for generating the command prompt. I don’t care about the command prompt here; the reason I’m setting PROMPT_COMMAND is to get the side effect of echoing the special characters needed to set the terminal title.

According to these pages, what I’m doing should work:
http://tldp.org/HOWTO/Xterm-Title-4.html
http://superuser.com/questions/79972/set-the-title-of-the-terminal-window-to-the-current-directory

Not according to the web pages I read (linked above). PROMPT_COMMAND is just something run before the prompt is generated, and doesn’t itself set the prompt. By echoing special character sequences, I can change the xterm title, or should be able to, according to those pages.

Wrong. See the two links I posted in the response to the other user. They should work. They are specific sequences to change the title.

When I echo that, it moves my cursor to a higher position in the terminal window, but doesn’t affect the title.

If I execute this in my bash shell, each command starts being written on top of the previous one (it no longer scrolls) and thus becomes corrupted and unusable. I had to kill off the xterm after doing this and start another one.

I thought you wanted to change the prompt, and that you should better do by setting PS1, not running echo via COMMAND_PROMPT.

According to these pages, what I’m doing should work:
http://tldp.org/HOWTO/Xterm-Title-4.html
http://superuser.com/questions/79972/set-the-title-of-the-terminal-window-to-the-current-directory

Ah, ok.
And it does work, at least if you rename /etc/bash.bashrc, something in there seems to override the window title. :wink:

Exactly. That’s what I wrote.

When I echo that, it moves my cursor to a higher position in the terminal window, but doesn’t affect the title.

I never said that this should change the title. It was an example that escape sequences work in general.

If I execute this in my bash shell, each command starts being written on top of the previous one (it no longer scrolls) and thus becomes corrupted and unusable. I had to kill off the xterm after doing this and start another one.

You could have just set PS1 to something else. No need to kill xterm… :wink:

PS: Type this into your xterm (or add it to .bashrc or similar) and you should be able to change the window title afterwards:

ppwd () { exit; }

This overrides the function that sets the window title.

Or you could set the window title directly with something like this (instead of setting PROMPT_COMMAND):

ppwd () { echo -ne "\033]0;${USER}@@${HOSTNAME}: ${PWD}\007"; }

Apparently ppwd() gets called before the prompt is printed, but after PROMPT_COMMAND. The default function (defined in /etc/bash.bashrc) sets the window title to the standard one (“user@host: dir”), which overrides the title you set via PROMPT_COMMAND.

Personally, I consider that a misfeature. Fortunately, I use “csh” rather than “bash”, so I don’t have to put up with it.

In any case, I opened an xterm with the title that I wanted (using “-title”). Then I ran “bash” in that xterm. And, sure enough, that misfeature did its annoying thing and changed the title.

This is with opensuse 13.2. I haven’t checked 13.1, though I’m pretty sure it does the same.

What this shows is that bash is still able to emit an escape sequence that tells “xterm” to change its title. So I don’t think a feature (or misfeature) is broken.

To be a bit pedantic, neither “xterm” nor “bash” is part of KDE. So it would not be a KDE feature.

If you undefine ppwd() in ~/.bashrc or /etc/bash.bashrc.local, the -title parameter works fine as well.

Actually this is no misfeature of bash, but just the standard openSUSE setup (as mentioned, ppwd() is defined in /etc/bash.bashrc to change the window title every time the shell prints a prompt).

Oh, and actually ppwd() is called by the prompt. See “echo $PS1”…
So setting PS1 to something else will prevent the window title from being changed as well, and will make your escape sequences for changing the title work.

I probably should have used a smiley. I did not say that it was a bash misfeature. It’s a “standard openSUSE setup” misfeature. It’s probably there for “csh” too. But I do a major edit on “/etc/csh.cshrc” to remove as much of the annoying stuff as I can.

I once tried to delete “/etc/csh.cshrc” and “/etc/csh.login”, but X failed to start. Maybe I should try that again – it’s been a few years since I last tried that.

Ok, but you can easily disable that misfeature… :wink:

It’s probably there for “csh” too. But I do a major edit on “/etc/csh.cshrc” to remove as much of the annoying stuff as I can.

Yes. The default csh.cshrc contains this:

## Prompting and Xterm title
#
set prompt="%B%m%b %C2%# "
if ( -o /dev/$tty && -c /dev/$tty ) then
  alias cwdcmd '(echo "Directory: $cwd" > /dev/$tty)'
  if ( -x /usr/bin/biff ) /usr/bin/biff y
  # If we're running under X11
  if ( ${?DISPLAY} ) then
    if ( ${?TERM} && ${?EMACS} == 0 && ${?MC_SID} == 0 && ! -r $HOME/.csh.expert ) then
      if ( ${TERM} == "xterm" ) then
        alias cwdcmd '(echo -n "\033]2;$USER on ${HOST}: $cwd\007\033]1;$HOST\007" > /dev/$tty)'
        cd .
      endif
    endif
    if ( -x /usr/bin/biff ) /usr/bin/biff n
    set prompt="%C2%# "
  endif
endif

So in csh you’d have to redefine/undefine the alias “cwdcmd”.

See also “man csh”:

       The  beepcmd,  cwdcmd,  periodic,  precmd,  postcmd,  and jobcmd **Special
       aliases** can be set, respectively, to execute  commands  when  the  shell
       wants  to  ring the bell, when the working directory changes, every **tpe-
       riod** minutes, before each prompt, before  each  command  gets  executed,
       after  each  command  gets  executed,  and  when  a job is started or is
       brought into the foreground.

I comment out that bunch of lines that you quoted.