PS1='${?#0}$ ' It uses a special form of parameter expansion, ${?#0}, which means: "Remove the character zero if it is the first character of ${?}, the exit code of the previous command."
You can also change the color of the prompt if the last exit code were not zero:
PS1='\[\e[0;$(($?==0?0:91))m\]$ \[\e[0m\]' That uses a if-else ternary expression $(($?==0?0:91)) — with some escaping — that makes the color code 0;91m (red, see color codes) if the last command exits with non-zero, or 0;0m (your default color) otherwise.


