|Bash Prompt HOWTO: $Revision: 0.100 $, $Date: 2009/08/07 02:33:22 $|
|Prev||Chapter 10. Prompt Code Snippets||Next|
Any command executed in Bash returns an error status, "0" for no problem and non-zero for an error. Non-zero values vary depending on the error and the program. The return status from the last command is stored in the shell variable "$?". Thanks to Antonio Macchi for sending in this colourised prompt depending on return status:
PS1="\[\e[\$(( (\$? == 0 ) ? 31 : 41 ))m\]\s-\v\\$\[\e[m\] "
Note that if you design a complex prompt around this functionality, $? will reflect the last Bash command - including any executed within the prompt. If this is a problem you can immediately capture $? to a variable before doing further prompt processing, and use the variable for colour tagging later.