Logo

How to change the output color of echo in Linux?

To print colored text in Linux using echo, you can utilize ANSI escape codes. Below is a quick example showing how to print “Hello World” in red, then reset the color to default:

echo -e "\033[31mHello World\033[0m"
  1. -e: Enables interpretation of backslash escapes in your shell (in Bash or similar).
  2. \033[31m: Sets the text color to red.
    • \033[ indicates the start of an ANSI sequence.
    • 31 is the code for red text (see below for more codes).
    • m applies that color to subsequent output.
  3. \033[0m: Resets the text formatting back to normal.

Common ANSI Color Codes

  • 30: Black
  • 31: Red
  • 32: Green
  • 33: Yellow
  • 34: Blue
  • 35: Magenta
  • 36: Cyan
  • 37: White
  • 0: Reset all attributes (return to default color)

More Examples

# Green text echo -e "\033[32mSuccess!\033[0m" # Yellow text with underline echo -e "\033[33;4mWarning!\033[0m" # Blue background, white text echo -e "\033[44;37mHello!\033[0m"
  • Multiple attributes can be combined with ; (e.g., \033[33;4m for yellow text plus underline).
  • If you want bold text, add 1 as in \033[1;31m for bold red.

Further Learning

If you want to strengthen your coding fundamentals (for tasks like this or more complex scripting) and dive deeper into data structures, algorithms, and coding interviews, here are two excellent resources from DesignGurus.io:

  1. Grokking Data Structures & Algorithms for Coding Interviews
    Gain a thorough understanding of crucial data structures and algorithmic techniques—key to efficient coding and interview success.

  2. Grokking the Coding Interview: Patterns for Coding Questions
    Learn the primary coding patterns that form the backbone of problem-solving in tech interviews.

By mastering these fundamentals, you’ll be better equipped to tackle both everyday scripting tasks and advanced technical challenges.

CONTRIBUTOR
TechGrind