Last Updated on February 25, 2024 by Mathew Diekhake
I would like to know how to change the colors displayed in bash, including colors to the bash console, colors to the nethack+windows 10+bash, and colors in vim bash. Please also teach me how to print colors to the console in bash. I would also like to invert the colors on putty terminal bash scripts. I also don’t understand why git bash has no colors and would like to get nice colors in git bash. Resolution:
The following tutorial demonstrates how to change bash colors for Linux distributions and any version of the Windows operating system that supports bash.
You can make parts of the bash script stand out by adding colors to it. This can help you easily identify parts of the code. You can choose to customize and colorize your bash script and prompt and choose to have those customizations for the one session or save it so it is there for next time as well.
There are two parts to bash colorization that you may be interested in: the bash script itself and the bash prompt. Thankfully both can be cutomized.
ANSI Escape Codes
If you want to print colors to the console bash, you can do that using ANSI escape codes. Here are the codes:
Black 0;30 Dark Gray 1;30
Red 0;31 Light Red 1;31
Green 0;32 Light Green 1;32
Brown/Orange 0;33 Yellow 1;33
Blue 0;34 Light Blue 1;34
Purple 0;35 Light Purple 1;35
Cyan 0;36 Light Cyan 1;36
Light Gray 0;37 White 1;37
An example of how you would use the ANSI escape codes in your script:
# .---------- constant part!
# vvvv vvvv-- the code from above
RED='\033[0;31m'
NC='\033[0m' # No Color
printf "I ${RED}love${NC} Stack Overflow\n"
The example above prints love
in red.
To Save Bash Customizations
Change the PS1 variable in the .bashrc file. To do this, scroll down to the PS1 variable and replace the default virable with the cusomized one.
Note: Enter the color under the if [ "$color_prompt" = yes ]; then
line and enter the variable without colors under the else
line.
Color Mode
There are four different modes with these codes. The modes are Color Mode, Text Mode, Foreground Mode, and Background Mode. The color mode allows you to add numbered codes to change the appearance of the colors (not the text).
Adding 0
resets the colors; adding 1;
makes the colors lighter than normal; and adding 2;
makes the colors darker than normal.
Text Mode
The Text Mode allows you to use numbered codes to change the appearance of the text (not the colors).
Add 3;
for italics; add 4;
to underline; add 5;
for a slow blink; add 6;
for a quick blink; add 7;
to reverse; add 8;
to hide; and add 9;
to cross out the text.
Bash Script Colors Not Working on MacOS
If you are trying to add colors to bash scripts but they are not working, it is likely because you are using \e
instead of \033
. This is espcially true for MacOS/Mac OSX users.
Related Tutorials