Set $PS1 differently on local computer and in ssh session

I keep my home directory under version control, so that my basic configuration is easily available on any computer.

My bash command prompt on my local computer is a complicated, colorful thing that includes the current git repository and its state. But I'd rather have a simple user@host ~ $ command prompt when I ssh into a remote machine.

I'd like to be able to update my .profile so that it sets up a complicated $PS1 when running locally and a simplified one when running in an ssh session.

Basically, I'd like something like an $AM_I_LOGGED_IN_VIA_SSH variable to test in my .profile. Is that possible?


When you login via SSH, several extra environment variables get set. You could use these as a test to set your PS1 in your .profile.

if [ -n "$SSH_CLIENT" ]; then
    PS1="Via ssh: "
else
    PS1="Local: "
fi

If you check your environment (env | grep SSH) you'll find several candidates such as $SSH_CLIENT, $SSH_CONNECTION, and $SSH_TTY; the exact list will depend on the sshd version.