I have a similar section in my .bashrc via a somewhat hackish loop (or two):
ESC=$'\x1b'
# Terminal color escapes
colornames=(BLACK RED GREEN YELLOW BLUE MAGENTA CYAN)
for i in ${!colornames[@]}; do
eval "${colornames[$i]}=\"${ESC}[3${i}m\""
eval "BG${colornames[$i]}=\"${ESC}[4${i}m\""
done
# There are holes in this sequence, so it doesn't loop nicely
BOLD="${ESC}[1m"
DARK="${ESC}[2m"
UNDERLINE="${ESC}[4m"
BLINK="${ESC}[5m"
INVERSE="${ESC}[7m"
RST="${ESC}[m"
styles=(BOLD DARK UNDERLINE BLINK INVERSE RST)
# and the PS1-safe versions of the above
for i in ${colornames[@]} ${colornames[@]/#/BG} ${styles[@]}; do
eval "PS$i=\"\\[\$$i\\]\""
done
Having color escapes in shell variables comes in handy from time to time (like faking 'colordiff' with sed).
I used to have a different prompt color when SSH_CONNECTION was in my environment, to distinguish local terminals from remote connections. And also root/non-root, although I didn't know about $EUID and used the less-effective $(id -u).
I do exactly this as well as add a little blurb in motd saysing that this machine is production, tread carefully. This seems to solve a great deal of mistaken identity problems.
Is there any way to set this on directory level? I do my development in my /home/XYZ folder, and production is in /usr/local/www/XYZ and it would be great if I could set the background to red in the production dir (if I happen to wander into it somehow (which I really almost never do)).
PS1='\[\e[1;31m\][\u@\h \W]\$\[\e[0m\] '
We use this in our production environments and the red prompt, though not as jarring as a red background, is still scary enough to serve its purpose.
One upside in setting this up on the server, as opposed to local like the OP, is that all connections in will get the red prompt.