Using the -e option with the echo command will enable the interpretation of backslash escapes. This gives you the option to echo colored text in the terminal. Here is an example. The red sections represent where the color starts and ends.
echo -e "\033[31mThis is some red text.\033[0m" This some normal text.
\033[31m is the code you use to make text red.
\033[0m is the code you use to reset the color back to normal.
You will notice the only part that changes is the number between the [ and the m. So the 31 is the part that determines the color is red and the 0 is the part that sets the color back to default. The quotes around the colored section are necessary for this example.
These colors can also be set as variables to make your text easier to edit. Here is an example of a bash script using these variables.
#!/bin/bash red='\033[31m' reset='\033[0m' echo -e ${red}This is some red text.${reset} This is some normal text.
Basic Colors
30-37 are some basic colors you can use.
black='\033[30m' red='\033[31m' green='\033[32m' yellow='\033[33m' blue='\033[34m' magenta='\033[35m' cyan='\033[36m' white='\033[37m'
Bright Colors
90-97 are “bright” versions of those colors.
bblack='\033[90m' bred='\033[91m' bgreen='\033[92m' byellow='\033[93m' bblue='\033[94m' bmagenta='\033[95m' bcyan='\033[96m' bwhite='\033[97m'
Highlight Colors
40-47 will allow you to highlight your text with a background color.
black='\033[40m' red='\033[41m' green='\033[42m' yellow='\033[43m' blue='\033[44m' magenta='\033[45m' cyan='\033[46m' white='\033[47m'
Bright Highlight Colors
100-107 are “bright” versions of those highlight colors.
bblack='\033[100m' bred='\033[101m' bgreen='\033[102m' byellow='\033[103m' bblue='\033[104m' bmagenta='\033[105m' bcyan='\033[106m' bwhite='\033[107m'
Other Text Effects
You can also add some other effects to make your text stand out. *Note – Be aware that bold text and “bright” text may look very similar.*
bold='\033[1m' faint='\033[2m' italic='\033[3m' underline='\033[4m' blink='\033[5m' reversed='\033[7m'
Multiple effects can be used together, just separate the numbers with a semicolon. This final example will echo text that is red, blinking, and underlined.
#!/bin/bash rbu='\033[31;5;4m' reset='\033[0m' echo -e ${rbu}This is some red text.${reset} This is some normal text.
Hopefully you found this tutorial useful. Now go color some text!