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: "
    PS1="Local: "

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.


If your sshd is not providing useful variables, you could use

ps -p $PPID

or some other random ps trick (if your shell doesn't set $PPID) to see if the shell's parent process is your remote-login-process or not. Then if parent-is-remote-thingie switch $PS1. And then download OpenSSH and use geekosaur/Caleb's suggestions because your ssh server is junk. ;)

Or, you can use the output of "who am i", grab the last field (ie, $( who am i | awk '$0=$NF') ), and check to see if the host is a remote one or local to decide if you logged in locally or remotely.

Or you can switch on the output of $(hostname) to have your shell use the appropriate $PS1 on specific hosts.

Or, let's really go nuts. If you're using a modern shell - like bash, ksh93, or zsh, you can take advantage of the fact that $PS1 is reevaluated upon execution. So, you could include an inline conditional block to do the crazy git stuff if your cwd is within the repository, and something else outside. For example, here I'm making my prompt change depending on the directory I'm in by using [[ conditional ]] && $( echo "whatever it should be if true) || $( echo "whatever should be the if-not-true prompt" ), basically.

default prompt$ PS1='$(id -un) @ $(hostname)$( [[ $(pwd) = /tmp ]] && echo " [I am in /tmp]" || echo " /not/in/tmp")$ '
myname @ host /not/in/tmp$ cd /tmp
myname @ host [I am in /tmp]$ cd /etc
myname @ host /not/in/tmp$

Sure, the $PS1 definition will look crazy in your profile, but you then only get super-colorful-git-stuff when you're actually in the git repository, while you ahve regular colors elsewhere. :) Note that you have to use single quotes around the PS1 assignment. You do not want the variables expanded when PS1 is assigned; you want them expanded when $PS1 is evaluated later. So, single-quotes.


If PermitUserEnvironment is enabled in sshd then you can use ~/.ssh/environment to create $PS1 and then just detect presence of a value in your script that normally sets it.


As an addition to Ignacio's answer, to make things clear, PermitUSerEnvironment option must be enabled on server side for this to work. If server doesn't enable this option, you won't have colorful ssh session with that server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.