Chapter 9. Loading a Different Prompt

9.1. Loading a Different Prompt, Later

The explanations in this HOWTO have shown how to make PS1 environment variables, or how to incorporate those PS1 and PS2 strings into functions that can be called by ~/.bashrc or ~/.bash_profile. I have mostly shown prompt setup wrapped in a function: to use this, edit your ~/.bashrc file and add the following (using our previous example prompt, "proml":

# .bashrc
#
# Leave any existing aliases, functions, etc. alone.

function proml
{
case $TERM in
    xterm*|rxvt*)
        local TITLEBAR='\[\033]0;\u@\h:\w\007\]'
        ;;
    *)
        local TITLEBAR=''
        ;;
esac

PS1="${TITLEBAR}\
[\D{%H%M}]\
[\u@\h:\w]\$ "
PS2='> '
PS4='+ '
}

proml
unset proml

Notice the last two lines: putting the "proml" function in .bashrc makes it available, but doesn't run it. We do that with the second last line. And finally, we clean up a bit by unsetting "proml" once it's loaded our PS1, PS2, and PS4 strings into the environment.

It's important to point out that you can do precisely the same thing without using functions at all:

# .bashrc
#
# Leave any existing aliases, functions, etc. alone.

case $TERM in
    xterm*|rxvt*)
        local TITLEBAR='\[\033]0;\u@\h:\w\007\]'
        ;;
    *)
        local TITLEBAR=''
        ;;
esac

PS1="${TITLEBAR}\
[\D{%H%M}]\
[\u@\h:\w]\$ "
PS2='> '
PS4='+ '

This will have exactly the same results. So why use functions? It's up to you. But I think it's a good to encapsulate the prompt code: you can easily export it to a separate file, and it's a bit easier to use the functions to load prompts on the fly.

See also Section 2.6 for Johan Kullstam's note regarding the importance of putting the PS? strings in ~/.bashrc .