7

I'm trying to change PS1 look based on what host I'm connected in using SSH. My current PS1:

PS1='\[\e[1;32m\]\u@\h\[\e[1;34m\] \w\[\e[1;31m\]$(__git_ps1)\[\e[1;0;37m\] \$\[\e[0m\] '

For host host1 I'd like to replace the first color with yellow which is 1;33 and for host2 take 1;35 as an example.

How can I figure out that I'm connected to the given host using SSH and alter PS1 accordingly?

3
  • 2
    In the remote hosts's shell initialisation file: if [[ -n $SSH_CLIENT ]]; then...
    – jasonwryan
    Commented Jul 20, 2015 at 22:43
  • That's sound like a good solution. What can I change part of the PS1 by extracting it to a variable? I've tried like COLOR='some color' and using it in PS1='$COLOR\u@\uh...' but it doesn't work.
    – tomrozb
    Commented Jul 20, 2015 at 23:00
  • Just use two PS1's: the first in the if condition with your SSH colours, the second is your normal prompt.
    – jasonwryan
    Commented Jul 20, 2015 at 23:08

2 Answers 2

13

Construct your prompt specification in pieces, or use intermediate variables, or a combination of both. SSH sets the SSH_CLIENT variable, which indicates where you're logged in from. You can then use the host name to determined where you're logged into.

if [[ -n $SSH_CLIENT ]]; then
  case $HOSTNAME in
    *.example.com) prompt_user_host_color='1;35';; # magenta on example.com
    *) prompt_user_host_color='1;33';; # yellow elsewhere
  esac
else
  unset prompt_user_host_color # omitted on the local machine
fi
if [[ -n $prompt_user_host_color ]]; then
  PS1='\[\e['$prompt_user_host_color'm\]\u@\h'
else
  PS1=
fi
PS1+='\[\e[1;34m\] \w\[\e[1;31m\]$(__git_ps1)\[\e[1;0;37m\] \$\[\e[0m\] '
8
  • Please add \[ ahead of \e, otherwise it won't work. Thanks for the solution, it's perfect!
    – tomrozb
    Commented Jul 21, 2015 at 18:54
  • You may remove the ;; trailing the unset command in the else block.
    – Stephane
    Commented Aug 5, 2017 at 13:09
  • I understand this configuration needs to be placed in the client .bashrc file. How can then it be used by the ssh client if there is a similar file on the server ?
    – Stephane
    Commented Aug 5, 2017 at 13:44
  • @Stephane I don't understand your question. This file is not used by the client, and it is not meant to be used on the client. Commented Aug 5, 2017 at 18:10
  • 2
    @Stephane Yes, the analysis of $HOSTNAME wouldn't be useful if the code was only ever placed on a single server. People who log in to multiple machines often replicate their configuration files to all of them. Commented Aug 6, 2017 at 7:06
2

Like the other one, but you could also use the separate rc file for ssh.

<<\SSH_RC \
    tee -a ~/.ssh/rc
case $HOSTNAME in
    (host1) sshclr=1;;
    (host2) sshclr=3;;
esac

...and wherever you are assigning your $PS1...

PS1="\[\e[1;$((32+ssh_clr))"'...

...you could leave it in single quotes, too...

PS1='\[\e[$((!$?|4));$((32+sshclr))...'

...which should underline if the last command executed exited with a non-zero exit code. Here's a picture...

enter image description here

I added the ${SSH_CLIENT+ssh:} expansion there to clearly denote @ssh: when connected that way...

You must log in to answer this question.

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