I've been trying to customize my Bash prompt so that it will look like
[feralin@localhost ~]$ _
with colors. I managed to get constant colors (the same colors every time I see the prompt), but I want the username ('feralin') to appear red, instead of green, if the last command had a nonzero exit status. I came up with:
e[1;33m[$(if [[ $? == 0 ]]; then echo "e[0;31m"; else echo "e[0;32m"; fi)ue[m@e[1;34mh e[0;35mWe[1;33m]$ e[m
However, from my observations, the $(if ...; fi)
seems to be evaluated once, when the .bashrc
is run, and the result is substituted forever after. This makes the name always green, even if the last exit code is nonzero (as in, echo $?
). Is this what is happening? Or is it simply something else wrong with my prompt? Long question short, how do I get my prompt to use the last exit code?