- Article pages now have a discussion option at the bottom (moderated/captcha, but no registration needed) - recently upgraded to a newer Dokuwiki version, please report problems

Add Color to your scripts

snipplet:
terminal,
color
LastUpdate:
2010-07-31
Contributor:
Frank Lazzarini
type:
snipplet
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.

Discussion

Enter your comment
 
snipplets/add_color_to_your_scripts.txt · Last modified: 2011/06/24 07:09 by thebonsai
GNU Free Documentation License 1.3
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0