Terminal output is plain by default. Adding an echo color code to your bash scripts makes warnings, errors, and status messages easier to distinguish at a glance. Linux gives you two ways to do this: ANSI escape sequences and the tput utility.
How Echo Color Code Works in Linux with ANSI Sequences
Every ANSI color code starts with an escape character, written as \033 (octal) or \x1B (hex). The terminal reads this character and applies the formatting that follows before printing the text.
The full pattern is:
\033[STYLE;COLORm
The -e flag on echo is required. Without it, the shell prints the escape characters literally instead of interpreting them. The echo command man page covers all supported flags and their behavior in detail.
ANSI Echo Color Code — Text Colors
Numbers 30–37 set the foreground (text) color. Pair them with a style modifier using a semicolon, then close with m.
30
31
32
Orange 33
34
35
36
37
| Color | Code |
|---|---|
| Black | 30 |
| Red | 31 |
| Green | 32 |
| Brown/Orange | 33 |
| Blue | 34 |
| Purple | 35 |
| Cyan | 36 |
| Light Gray | 37 |
Style Modifiers
The first number in the sequence sets appearance. Normal (0) is the default. Bold (1) and underlined (4) are the most widely supported across terminals.
| Appearance | Code |
|---|---|
| Normal | 0 |
| Bold | 1 |
| Dim | 2 |
| Underlined | 4 |
| Blinking | 5 |
| Hidden | 8 |
| Strikethrough | 9 |
Background Color Codes
To color the background instead of the text, add 10 to any text color code. Red text is 31, so a red background is 41. The same rule applies across all eight base colors.
40
41
42
Orange 43
44
45
46
47
A script that prints red text on a white background looks like this:
RED_ON_WHITE='\033[0;31;47m' RESET='\033[0m' echo -e "${RED_ON_WHITE}Warning: disk usage above 90%${RESET}"
The reset sequence \033[0m clears all active formatting. Without it, the color bleeds into every line that follows. The terminal-colors.d configuration reference explains how systemd utilities handle scheme-based color overrides at the OS level.
Applying an Echo Color Code Using tput
The tput utility reads your terminal’s capability database and sends the correct control sequences automatically. It’s more portable than raw ANSI codes and reads more clearly in scripts.
tput setaf 1 # red text tput setab 4 # blue background tput bold # bold tput sgr0 # reset all styling
setaf accepts values from 0 to 255, so you have access to a much wider range than the basic eight ANSI codes. Run a loop from 0 to 255 to preview all available shades in your terminal.
tput Sub-command Reference
| Action | Sub-command |
|---|---|
| Font color (0–255) | setaf |
| Background color (0–255) | setab |
| Bold | bold |
| Dim | dim |
| Underlined | smul |
| Blink | blink |
| Reset all | sgr0 |
The full list of terminal capabilities — including cursor movement and screen clearing — is documented on the tput man page.
Storing Echo Color Codes as Variables in Bash Scripts
Inline escape sequences get hard to read once a script grows past a few dozen lines. Defining colors as variables at the top keeps everything clean.
RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' RESET='\033[0m' echo -e "${GREEN}Build passed${RESET}" echo -e "${RED}Error: file not found${RESET}" echo -e "${YELLOW}Warning: low memory${RESET}"
This pattern makes it easy to swap a color across the entire script in one edit. Always pair each colored section with RESET at the end of the string, or trailing lines will inherit the last active color.
[ -t 1 ] first to confirm stdout is a terminal before applying color.
FAQs
What is an echo color code in Linux?
An echo color code is an ANSI escape sequence that tells the terminal to render text in a specific color or style. You pass it through echo -e with \033[ followed by a style and color number.
Why does echo not display colors in Linux?
You likely forgot the -e flag. Without it, echo prints escape characters as literal text. Some shells also require $'...' quoting syntax to interpret \033 correctly.
What is the difference between ANSI codes and tput for terminal colors?
ANSI codes are compact and inline. tput reads your terminal’s capability database, which makes it more portable across different terminal emulators. Both achieve the same result in most modern environments.
How do I reset the echo color after printing?
Append \033[0m (ANSI) or run tput sgr0 at the end of each colored output. Without a reset, all subsequent terminal output inherits the last active color.
Can I use echo color codes in a bash script?
Yes. Define color variables at the top of the script with ANSI sequences, then reference them with echo -e. Always include a reset variable to stop the color after each message.