While taking a look at this awesome thread I noticed that some examples use
PS1=\"Blah Blah Blah\"
and some use
PROMPT_CO
The difference is that PS1 is the actual prompt string used, and PROMPT_COMMAND is a command that is executed just before the prompt. If you want the simplest, most flexible way of building a prompt, try this:
Put this in your .bashrc:
function prompt_command {
export PS1=$(~/bin/bash_prompt)
}
export PROMPT_COMMAND=prompt_command
Then write a script (bash, perl, ruby: your choice), and place it in ~/bin/bash_prompt.
The script can use any information it likes to construct a prompt. This is much simpler IMO because you don't have to learn the somewhat baroque substitution language that was developed just for the PS1 variable.
You might think that you could do the same by simply setting PROMPT_COMMAND directly to ~/bin/bash_prompt, and setting PS1 to the empty string. This at first appears to work, but you soon discover that the readline code expects PS1 to be set to the actual prompt, and when you scroll backwords in history, things get messed up as a result. This workaround causes PS1 to always reflect the latest prompt (since the function sets the actual PS1 used by the invoking instance of the shell), and this makes readline and command history work fine.