[Melbourne-pm] Bash prompt red on error

Hamish Carpenter hamish at hamishcarpenter.com
Wed May 12 20:44:33 PDT 2010


As briefly discussed last night, I mentioned I'd changed my prompt to
be red on error (return code != 0). This helps me detect failed
commands and also helps in another case where you type a command then
cancel it by pressing ctrl+c. This was what prompted me to change my
prompt as someone watching your terminal cannot easily tell that
command was canceled, especially if it is a command that would have no
output on success (eg rf -f project_directory).

add the following to your .bash_profile:

# needs PS1 to be previously defined so add nearer the end of your
.bashrc/.bash_profile
# will turn entire prompt red (31) or green (32)
# depending on previous output return code
export PS1="\[\e[\`if [ \$? = 0 ]; then echo '0;32m'; else echo
'0;31m'; fi\`\]$PS1\[\e[0m\]"


hth,

Hamish


More information about the Melbourne-pm mailing list