tags:

views:

46

answers:

2

While taking a look at this awesome thread I noticed that some examples use

PS1="Blah Blah Blah"

and some use

PROMPT_COMMAND="Blah Blah Blah"

(and some use both) when setting the prompt in a bash shell. What is the difference between the two? An SO search and even a bit of broader google searching aren't getting me results, so even a link to the right place to look for the answer would be appreciated. Thanks!

+2  A: 

From the GNU Bash doc page: http://www.gnu.org/software/bash/manual/bashref.html

PROMPT_COMMAND
    If set, the value is interpreted as a command to execute before
    the printing of each primary prompt ($PS1).

I never used it, but I could have used this back when I only had sh.

Scott Thomson
+1  A: 

PROMPT_COMMAND can contain ordinary bash statements whereas the PS1 variable can also contain the special characters, such as '\h' for hostname, in the variable.

For example here is my bash prompt that uses both PROMPT_COMMAND and PS1. The bash code in PROMPT_COMMAND works out what git branch you might be in and displays that at the prompt, along with the exit status of the last run process, hostname and basename of the pwd.

PROMPT_COMMAND='RET=$?;\
  BRANCH="";\
  ERRMSG="";\
  if [[ $RET != 0 ]]; then\
    ERRMSG=" $RET";\
  fi;\
  git branch 2>/dev/null 1>/dev/null;\
  if [[ $? == 0 ]]; then\
    BRANCH=$(git branch 2>/dev/null | grep * |  cut -d " " -f 2);\
  fi;\
  PS1="$GREEN\u@\h $BLUE\W $CYAN$BRANCH$RED$ERRMSG \$ $LIGHT_GRAY";'

sashang