snipplet : terminal , color Contributor : Frank Lazzarini , Dan Douglas type : snipplet

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 snipplet sets up associative arrays for basic color codes using tput for Bash, ksh93 or zsh. You can pass it variable names to correspond with a collection of codes. There's a main function with example usage.