Close Menu
    Facebook X (Twitter) Instagram
    Command Linux
    • About
    • How to
      • Q&A
    • OS
      • Windows
      • Arch Linux
    • AI
    • Gaming
      • Easter Eggs
    • Statistics
    • Blog
      • Featured
    • MORE
      • IP Address
      • Man Pages
    • Write For Us
    • Contact
    Command Linux
    Home - Q&A - Echo Color Code in Linux

    Echo Color Code in Linux

    WillieBy WillieMarch 16, 2026Updated:March 27, 2026No Comments4 Mins Read

    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.

    Black
    30
    Red
    31
    Green
    32
    Brown/
    Orange 33
    Blue
    34
    Purple
    35
    Cyan
    36
    Lt. Gray
    37
    ColorCode
    Black30
    Red31
    Green32
    Brown/Orange33
    Blue34
    Purple35
    Cyan36
    Light Gray37

    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.

    AppearanceCode
    Normal0
    Bold1
    Dim2
    Underlined4
    Blinking5
    Hidden8
    Strikethrough9

    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.

    Black
    40
    Red
    41
    Green
    42
    Brown/
    Orange 43
    Blue
    44
    Purple
    45
    Cyan
    46
    Lt. Gray
    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

    setaf
    256 colors
    setab
    256 colors
    bold
    on/off
    dim
    on/off
    smul
    on/off
    blink
    on/off
    sgr0
    reset all
    ActionSub-command
    Font color (0–255)setaf
    Background color (0–255)setab
    Boldbold
    Dimdim
    Underlinedsmul
    Blinkblink
    Reset allsgr0

    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.

    When a script runs inside a CI pipeline or gets redirected to a log file, ANSI codes appear as raw characters. Check [ -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.

    Willie
    • Website

    Willie has over 15 years of experience in Linux system administration and DevOps. After managing infrastructure for startups and enterprises alike, he founded Command Linux to share the practical knowledge he wished he had when starting out. He oversees content strategy and contributes guides on server management, automation, and security.

    Related Posts

    Copy File Using CP Command In Linux

    April 16, 2026

    How To Find Text in Files In Linux

    April 14, 2026

    Ubuntu Install AppImage On Linux

    April 14, 2026

    How to Check Linux Version

    April 9, 2026
    Top Posts

    FBREPLACEFR

    February 21, 2026

    Arch KDE Installation On Linux

    January 14, 2026

    Echo Color Code in Linux

    March 16, 2026

    How To Fix errordomain=nscocoaerrordomain&errormessage=could not find the specified shortcut.&errorcode=4

    December 2, 2025
    • Home
    • Contact Us
    • Privacy Policy
    • Terms of Use

    Type above and press Enter to search. Press Esc to cancel.