10.3. Return Status

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.