System: Ubuntu 20.04

My goal: change terminal text color when I connect to SSH host and reset the terminal text color once the SSH connection is closed.

Status: I managed to change the text color when I establish the SSH connection (--> How to change terminal colors when connecting to SSH hosts)


[ -n "$SSH_CONNECTION" ] && echo -e  '\033]10;#ff0000\007'

Problem: The terminal text color is not reset once I close the SSH connection.

Can this be done by editing .bash_profile? How can this be done not following the approach used in the mentioned article?

1 Answer 1


If you only need it to work for non-nested SSH sessions, you could put a corresponding line in your ~/.bash_logout file, for example

[ -n "$SSH_CONNECTION" ] && echo -e  '\033]10;#ffffff\007'

will set the foreground color to white on exit.

  • that's it....awesome and pretty easy! and btw. I use nested SSH sessions. I added the line in the ~./bash_logout as proposed on the remote host and on exit the text color is reset. Do you know by the way what those parameter / command switch like "- n", "-f"... mean?
    – LaUs3r
    Sep 23, 2020 at 20:10
  • @LaUs3r if you're referring to the -n in the [ ... ] test brackets, they're all documented in the bash shell's internal help under help test. In the case of -n specifically, it tests for a non-empty string. Sep 23, 2020 at 21:54
  • exactly that's what I'm referring to. I will go study that now :)
    – LaUs3r
    Sep 24, 2020 at 8:29
  • What I learnt is, that once you put something into the .bash_profile, commands from .bashrcare not executed. Originally I had neofetch executed everytime I start the bash by putting it at the end of .bashrc. Once I created the .bash_profile, it didn't work anymore. I needed to move the line from .bashrc to `.bash_profile
    – LaUs3r
    Sep 24, 2020 at 8:34
  • @LaUs3r if ~/.bash_profile exists, bash uses it in preference to ~/.profile. In Ubuntu, the default ~/.profile conditionally sources ~/.bashrc. If you want the same behavior when ~/.profile exists, consider including (i.e. sourcing) $HOME/.profile from your ~/.bash_profile Sep 24, 2020 at 11:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .