How to change the output color of echo in Linux
I am trying to print a text in a red color in the terminal using the echo command. How can I do that?
You can change the output color of echo in Linux using ANSI escape codes. To print text in red, you need to use the escape sequence \033[31m before your text and \033[0m after to reset the color. For example: echo -e "\033[31mThis text will appear in red\033[0m".
Contents
- Basic ANSI Escape Codes for Colors
- Working with echo Command
- Color Table and Examples
- Advanced Formatting Options
- Permanent Color Solutions
- Cross-Platform Considerations
Basic ANSI Escape Codes for Colors
ANSI escape codes are special sequences of characters that control terminal formatting. In Linux terminals, these codes can change text color, background color, and other text attributes.
The basic format for ANSI color codes is:
\033[<code>m
Where:
\033represents the escape character (can also be written as\eor\x1b)<code>is the color or formatting codemends the sequence
For red text, you use:
\033[31m
To reset all formatting back to normal:
\033[0m
Working with echo Command
The echo command needs the -e flag to interpret escape sequences. Here’s the basic syntax for colored text:
echo -e "\033[31mYour red text here\033[0m"
Key points about using echo with colors:
- Always include
-eflag to enable escape sequence interpretation - Always end with
\033[0mto reset formatting - The codes work in most modern Linux terminals (xterm, gnome-terminal, konsole, etc.)
- Some systems may require different escape sequences
Important: If your text doesn’t display colors, try using
\einstead of\033:bashecho -e "\e[31mRed text\e[0m"
Color Table and Examples
Here’s a comprehensive table of common ANSI color codes:
| Color | Text Code | Background Code | Example |
|---|---|---|---|
| Red | 31 |
41 |
echo -e "\033[31mRed text\033[0m" |
| Green | 32 |
42 |
echo -e "\033[32mGreen text\033[0m" |
| Yellow | 33 |
43 |
echo -e "\033[33mYellow text\033[0m" |
| Blue | 34 |
44 |
echo -e "\033[34mBlue text\033[0m" |
| Magenta | 35 |
45 |
echo -e "\033[35mMagenta text\033[0m" |
| Cyan | 36 |
46 |
echo -e "\033[36mCyan text\033[0m" |
| White | 37 |
47 |
echo -e "\033[37mWhite text\033[0m" |
| Black | 30 |
40 |
echo -e "\033[30mBlack text\033[0m" |
Practical Examples:
# Red text
echo -e "\033[31mThis is an error message\033[0m"
# Red background with white text
echo -e "\033[47m\033[31mWarning: Something went wrong!\033[0m"
# Multiple colors in one line
echo -e "\033[32mSuccess:\033[0m \033[33mOperation completed\033[0m"
Advanced Formatting Options
Beyond basic colors, ANSI escape codes support various text attributes:
| Attribute | Code | Example |
|---|---|---|
| Bold | 1 |
echo -e "\033[1mBold text\033[0m" |
| Dim | 2 |
echo -e "\033[2mDim text\033[0m" |
| Underline | 4 |
echo -e "\033[4mUnderlined text\033[0m" |
| Blink | 5 |
echo -e "\033[5mBlinking text\033[0m" |
| Reverse | 7 |
echo -e "\033[7mReversed colors\033[0m" |
| Hidden | 8 |
echo -e "\033[8mHidden text\033[0m" |
Combining Colors and Attributes:
# Bold red text
echo -e "\033[1;31mBold red error message\033[0m"
# Underlined blue text
echo -e "\033[4;34mImportant information\033[0m"
# Blinking yellow warning
echo -e "\033[5;33mWarning: System overload!\033[0m"
You can combine multiple codes by separating them with semicolons (;).
Permanent Color Solutions
If you frequently use colored output, consider these solutions:
1. Create Aliases in .bashrc or .zshrc:
# In ~/.bashrc or ~/.zshrc
alias red_echo='echo -e "\033[31m"'
alias reset_echo='echo -e "\033[0m"'
# Usage
red_echo "This is red text"; reset_echo
2. Create a Color Functions Script:
# Save as ~/bin/colors.sh
#!/bin/bash
red() { echo -e "\033[31m$1\033[0m"; }
green() { echo -e "\033[32m$1\033[0m"; }
yellow() { echo -e "\033[33m$1\033[0m"; }
blue() { echo -e "\033[34m$1\033[0m"; }
# Usage
source ~/bin/colors.sh
red "Error occurred"
green "Success!"
3. Use tput for Better Portability:
#!/bin/bash
# Using tput for more portable color codes
red=$(tput setaf 1)
green=$(tput setaf 2)
reset=$(tput sgr0)
echo "${red}This is red text${reset}"
echo "${green}This is green text${reset}"
Cross-Platform Considerations
Testing for Terminal Support:
#!/bin/bash
# Check if terminal supports colors
if [ -t 1 ] && [ "$TERM" != "dumb" ]; then
# Colors supported
red='\033[0;31m'
green='\033[0;32m'
reset='\033[0m'
else
# No color support
red=''
green=''
reset=''
fi
echo "${red}Red text${reset}"
echo "${green}Green text${reset}"
Handling Different Terminal Emulators:
Some terminals may have different color capabilities:
#!/bin/bash
# Terminal-specific color settings
case "$TERM" in
xterm*|rxvt*|gnome*|konsole*|mate*)
red='\033[31m'
;;
*)
red=''
;;
esac
echo "${red}Color may or may not work here${reset}"
Conclusion
- Use ANSI escape codes with echo and the
-eflag to change text colors in Linux terminals - For red text specifically:
echo -e "\033[31mYour text\033[0m" - Always reset colors with
\033[0mto avoid affecting subsequent output - Consider creating aliases or functions for frequently used colors
- Test for terminal color support to ensure cross-platform compatibility
- Combine color codes with formatting attributes like bold or underline for richer output
The echo command with ANSI escape codes provides a simple yet powerful way to add color to your terminal output, making scripts more readable and user-friendly. Experiment with different combinations to create visually appealing command-line interfaces!
Sources
Note: The web search attempts returned 429 Too Many Requests errors, so the following information is based on standard Linux terminal practices: