Use PS1 setting to set different terminal background color over ssh

2k views Asked by At

I want to use the PS1 setting in my .bashrc file to change the color of the terminal based on whether I'm on my local machine or using ssh.

My current .bashrc file on both my local machine and the ssh server is (the default): PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

I've tried adding "\e[40m" to the end, but only changes part of the background of the terminal, leaving a black bar in the middle

How would I go about changing the PS1 setting to, say, my local terminal is dark blue and the ssh terminal is dark gray?

Thanks!

1

There are 1 answers

8
David W. On

Just out of curiosity, doesn't that remote machine set its own PS1 value which means however you set your prompt locally, that remote machine will override it?

One way around this is to setup a function to replace your actual ssh command. Have that function set the color of your terminal, and then run the actual ssh command:

function ssh_function
{
    printf "\e[40m\e[37m"    # Grey on black
    clear
    \ssh $@ || read           # Actual ssh command
    printf "\e[0m"           # Reset terminal
    clear
}

Now, create an alias:

alias ssh="ssh_function"

Now, when you run ssh, it will run your ssh_function which sets the screen color before executing ssh and then resets your screen colors once out of ssh. The clear is to clear your terminal, so you get a constant color. Otherwise, it merely resets the color at your prompt.

And then hope that the remote PS1 environment variable doesn't reset the terminal color on you.