Show last command exit value in Bash shell prompt

Stick something like this in your .bashrc file:

# set PS1, last exit code if != 0, and PWD, with colors
PRE='\[\033['
POST='m\]'
COL_LASTEXIT="${PRE}01;41;37${POST}" # last command exit value
COL_PWD="${PRE}00;01;36${POST}" # current directory
COL_DEFAULT="${PRE}00${POST}" # back to default colors
export PS1="$COL_LASTEXIT\${?#0}$COL_PWD \w$COL_DEFAULT\\n\\\$ "

Command exit values other than zero are displayed in the subsequent prompt:

Check out the little red '1'

Check out the little red '1'

It’s the

${?#0}

bit which includes the last command exit value in the prompt, unless the value is zero, in which case nothing is displayed.

2 thoughts on “Show last command exit value in Bash shell prompt

  1. Below follows a minimal change to the bash prompt. Just add the conditional red result at the front.

    PRE=’\[33['
    POST='m\]‘
    COL_LASTEXIT=”${PRE}01;41;37${POST}” # last command exit value
    COL_DEFAULT=”${PRE}00${POST}” # back to default colors
    export PS1=”$COL_LASTEXIT\${?#0}$COL_DEFAULT\[33[01;32m\]\u@\h\[33[01;34m\] \w \$\[33[00m\] “

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>