snipplets:add_color_to_your_scripts

This is an old revision of the document!


Add Color to your scripts

snipplet:
terminal, color
LastUpdate:
2010-07-31
Contributor:
Frank Lazzarini
Keywords: color,echo
Contributor: Frank Lazzarini

Make your scripts output more readable using bash colors. Simply add these variables to your script, and you will be able to echo in color. (I haven't added all the colors available, just some basics)

# Colors
ESC_SEQ="\x1b["
COL_RESET=$ESC_SEQ"39;49;00m"
COL_RED=$ESC_SEQ"31;01m"
COL_GREEN=$ESC_SEQ"32;01m"
COL_YELLOW=$ESC_SEQ"33;01m"
COL_BLUE=$ESC_SEQ"34;01m"
COL_MAGENTA=$ESC_SEQ"35;01m"
COL_CYAN=$ESC_SEQ"36;01m"

Now if you want to output some text in color use echo -e instead of just echo. And always remember to use the $COL_RESET variable to reset the color changes in bash. Like so ….

echo -e "$COL_RED This is red $COL_RESET"
echo -e "$COL_BLUE This is blue $COL_RESET"
echo -e "$COL_YELLOW This is yellow $COL_RESET"

But also see the notes in the article about using terminalcodes about generating codes and hardwiring codes.

This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information
You could leave a comment if you were logged in.
  • snipplets/add_color_to_your_scripts.1280566691.txt
  • Last modified: 2011/06/24 05:09
  • (external edit)