Change bash prompt color on production and staging

Ken Andries • September 17, 2023

Having to manage multiple server environments, customizing Bash prompt colors may seem like a simple cosmetic change, It can save you from accidental mistakes running commands on the wrong environment or even worse having run sudo -i and having forget to return to the normal user

This is why I prefer to visually see the between staging and production servers, Using red for production, yellow/orange for staging and if I had to manage development servers I would use green.

Bash Prompt

In order to do this I set PS1 bash variable inside .bashrc I also change the /root/.bashrc file to update the one used as root user. You can also try them out by running the commands below

Setting red foreground(text) color for normal access on production server:

export PS1='\[\e[0;31m\][PROD] \u@\h:\w\$\[\e[0m\] '

Setting red background with white foreground color when using root user:

export PS1='\[\e[41;97m\][PROD] \u@\h:\w\$\[\e[0m\] '

Staging I want orange / yellow-ish color I (on my theme this looks like the image above):

export PS1='\[\e[0;33m\][STAGING] \u@\h:\w\$\[\e[0m\] '

For root users on staging I use the following prompt:

export PS1='\[\e[43;97m\][STAGING] \u@\h:\w\$\[\e[0m\] '

The colors are defined based on background starting with 3 and background starting with 4. Then using the following list to define the colors

The only exception is white which uses 97 for forground (text) and 107 for background color.

example to use red as a foreground color use 31m. when you want to change to a red background with yellow text you can use 41;33m.