This is what I use in my
(( e )) && printf "$e|"
ibug@example:~ $ false
1|ibug@example:~ $ ^C
130|ibug@exame:~ $ true
This has the advantage over JoL's answer that the value of
$? is preserved after displaying (via
return $e). It's also basically a rework of Android's default
/system/etc/mkshrc that does the same job.
$(PS1_PROMPT) is enclosed in single quotes because
$PS1 is evaluated every time it's printed, in addition to when set in a variable assignment expression, so single quotes prevents it from being evaluted right now and instead defers that to when printing.
Alternatively, as suggested by ilkkachu, you can make use of Bash
PROMPT_COMMAND special variable:
(( e )) && PROMPT_ECODE="$e|"
This has one advantage that no subshell is spawned every time PS1 is printed.