4

I want to change the color of the terminal display when entering a SSH session. In the .bashrc I have a snip that changes the prompt when logging in via SSH, but I need to change the whole background color.

The background will apply only when I'm logged into a remote host via SSH.

This thread has a good discussion on changing colors of the terminal display.

Changing colour of text and background of terminal?

For example the following printf lines in a bash script do exactly what I want - change the color of the line responding to the command and all subsequent lines of that terminal session.

However when included in the .bashrc as below,the effect lasts only for the first line of the session.

if [[ -z "$SSH_CLIENT" ]]
  then
  : 
else
   PS1="\[\e[01;36m\]$PS1\[\e[00m\]"
   printf '\e[48;5;125m'
   printf '\e[38;5;255m'
fi

Where 48 and 38 select foreground and background, the 5 selects the color table to use (in this case ANSI), and nnnm selects the color from the selected color table.

How can I make the change stick through the terminal session.

Stephen Boston
  • 3,814
  • 7
  • 39
  • 75
  • 2
    Possible duplicate of [How to set built-in color scheme for gnome-terminal via CLI in Ubuntu 16?](https://askubuntu.com/questions/803230/how-to-set-built-in-color-scheme-for-gnome-terminal-via-cli-in-ubuntu-16) – WinEunuuchs2Unix Dec 23 '18 at 19:33
  • @Win that solution doesn't let you set the default colors, so that won't work. – wjandrea Dec 23 '18 at 21:15
  • What version of Ubuntu are you using? If it's 14.04, this is a duplicate of [How can I set the background color of gnome-terminal using gconftool-2?](https://askubuntu.com/q/628122/301745) If it's 18.04, it's almost a duplicate of [Edit Gnome-Terminal profile from command line on 18.04](https://askubuntu.com/q/1068595/301745) – wjandrea Dec 23 '18 at 21:16
  • @wjandrea I have changed the question. I hope it is more clear. – Stephen Boston Dec 23 '18 at 21:21
  • @Stephen I posted a comment here before but I had a few details wrong so I removed it. In fact the only detail missing is what version of Ubuntu you're using (on the local machine). – wjandrea Dec 23 '18 at 21:23
  • Check your `$PS1` value - it might reset the colors after a prompt with color. `man bash` – waltinator Dec 23 '18 at 21:34
  • @wjandrea I have hosts running 19.04, 18.10,18.04,16.04 all MATE. I want this to work on all of them so that any connection between any two will have the same effect. The Mate terminal is a gnome terminal fork I believe. I haven't encountered any differences so far. – Stephen Boston Dec 23 '18 at 21:35
  • @waltinator Pretty much anything with colorized output will reset the colors when it's done printing. For example try `printf '\e[42m'; pip show; echo hi` – wjandrea Dec 23 '18 at 21:39
  • Look at the `~/.bashrc` files on the remote systems for `PS1` setting. Use a simple `if [[ -z "$SSH_CLIENT ]] ; then` (`SSH_CLIENT` is set if you logged in via `ssh`) – waltinator Dec 23 '18 at 21:44
  • You might be able to use an OSC sequence. See [egmont's answer here](https://unix.stackexchange.com/a/386027/117037). Can't confirm cause I'm not at my machine rn. – wjandrea Dec 23 '18 at 21:45
  • I have added the snip from my .bashrc. Note that the two printf lines when issued from the command line or included in a bash script have a persistent effect. Thank you @waltinator for the much nicer SSH check. All hosts have the same .bashrc for the user. – Stephen Boston Dec 23 '18 at 21:50

0 Answers0