Exit code display in Bash prompt

The normal Bash prompt

The standard Bash prompt looks like

bob@bobsbox:~$ echo $PS1
\u@\h:\w\$ 

or on a Debian based system, it also has the handy hackable prefix indicator

bob@bobsbox:~$ debian_chroot='boxed in'
(boxed in)bob@bobsbox:~$ echo $PS1
${debian_chroot:+($debian_chroot)}\u@\h:\w\$ 

Did I break it?

With the normal Bash prompt, it’s hard to tell when the previous command broke if it doesn’t give a meaningful error message.

(boxed in)bob@bobsbox:~$ true
(boxed in)bob@bobsbox:~$ false
(boxed in)bob@bobsbox:~$ 

so I like to change mine,

(boxed in)bob@bobsbox:~$ debian_chroot=
bob@bobsbox:~$ PS1='${?/#0/\u}@\h:\w\$ '
bob@bobsbox:~$ false
1@bobsbox:~$ true
bob@bobsbox:~$ 

It can even take a splash of colour,

bob@bobsbox:~$ PS1='\[\e[31m\]${?/#0/\[\e[00m\]\u}@\h\[\e[00m\]:\w\$ '
bob@bobsbox:~$ false
1@bobsbox:~$ true
bob@bobsbox:~$ 

…though how I convince Markdown to render the red 1@bobsbox in a fenced code block is mystery for another day.

See also