13

Starting on OS X, I ssh into foo (a Linux box). My terminal title changes to foo. I then ssh into bar (another Linux box). My terminal title changes to bar. I log out of bar. My terminal title changes to foo. I log out of foo. My terminal title stays foo (rather than changing back to the original title). The problem is obviously not with the terminal, since it works correctly when logging out of a Linux box back to a Linux box. It probably isn't ssh as I have the same problem with vim. In my mind that leaves the shell. I am using BASH. Looking through the environment I don't see a lot of difference between the Linux box and my OS X box. Both are using a TERM of xterm.

Chas. Owens
  • 2,682

2 Answers2

14

Adding

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

to my .bash_profile fixed the problem.

It appears as if the string in PROMPT_COMMAND gets executed every time the prompt is displayed. From the man page:

PROMPT_COMMAND

If set, the value is executed as a command prior to issuing each primary prompt.

elitalon
  • 375
Chas. Owens
  • 2,682
1

I've found better solution for this issue. OSX uses this system wide configuration:

PROMPT_COMMAND="update_terminal_cwd${PROMPT_COMMAND:+; $PROMPT_COMMAND}"

Function update_terminal_cmd is declared in /etc/bashrc_Apple_Terminal. Therefore, if you do not want to break your Terminal settings by resetting PROMPT_COMMAND, you can add these lines to the end of your ~/.profile or ~/.bashrc:

my_update_terminal_cwd() {
    update_terminal_cwd # call system wide function
    printf '\e]0;\a'    # reset additional title after SSH session
}

PROMPT_COMMAND="my_update_terminal_cwd${PROMPT_COMMAND:+; $PROMPT_COMMAND}"

Example:

Before SSH session

SSH session in progress - title set to remote hostname

After SSH session - title fixed

jous
  • 817