Bash PROMPT_COMMAND variable with examples 

Joined:
04/19/2010
Posts:
94

May 06, 2011 12:26:14    Last update: May 06, 2011 12:26:14
The bash environment variable PROMPT_COMMAND contains a regular bash command that is executed just before the command prompt is displayed.

For example:
$ export PROMPT_COMMAND=a
bash: a: command not found...
$ 
bash: a: command not found...
$ 
bash: a: command not found...
$ 

The command a is not valid so you get the error message every time you hit enter.

Echo something before $PS1:
$ export PROMPT_COMMAND='echo -n Hi!'
Hi!$ 
Hi!$ 
Hi!$ 


PROMPT_COMMAND is regularly used to change the xterm window title. You may find this in /etc/bashrc:
case $TERM in
    xterm*)
        if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
            PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
        else
            PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
        fi
        ;;
    screen)
        if [ -e /etc/sysconfig/bash-prompt-screen ]; then
            PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
        else
            PROMPT_COMMAND='printf "\033]0;%s@%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
        fi
        ;;
    *)
        [ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default
        ;;
esac
Share |
| Comment  | Tags