Adding Color Escape Codes to global CC array for use by echo -e

declare -ax CC; for i in `seq 0 7`;do ii=$(($i+7)); CC[$i]="\033[1;3${i}m"; CC[$ii]="\033[0;3${i}m"; done
I was looking for the fastest way to create a bunch of ansi escapes for use in echo -e commands throughout a lot of my shell scripts. This is what I came up with, and I actually stick that loop command in a function and then just call that at the beginning of my scripts to not clutter the environment with these escape codes, which can wreck havok on my terminal when I'm dumping the environment. More of a cool way to store escape ansi codes in an array. You can echo them like: echo -e "${CC[15]}This text is black on bright green background." I usually just use with a function: # setup_colors - Adds colors to array CC for global use # 30 - Black, 31 - Red, 32 - Green, 33 - Yellow, 34 - Blue, 35 - Magenta, 36 - Blue/Green, 37 - White, 30/42 - Black on Green '30\;42' function setup_colors(){ declare -ax CC; for i in `seq 0 7`;do ii=$(($i+7));CC[$i]="\033[1;3${i}m";CC[$ii]="\033[0;3${i}m";done;CC[15]="\033[30;42m"; export R='\033[0;00m';export X="\033[1;37m"; }; export -f setup_colors CC[15] has a background of bright green which is why it is separate. R resets everything, and X is my default font of bright white. CC[15]="\033[30;42m"; R=$'\033[0;00m'; X=$'\033[1;37m' Those are just my favorite colors that I often use in my scripts. You can test which colors by running for i in $(seq 0 $((${#CC[@]} - 1))); do echo -e "${CC[$i]}[$i]\n$R"; done See: http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html for more usage.
Sample Output
CC=([0]="\\033[1;30m" [1]="\\033[1;31m" [2]="\\033[1;32m" [3]="\\033[1;33m" [4]="\\033[1;34m" [5]="\\033[1;35m" [6]="\\033[1;36m" [7]="\\033[1;37m" [8]="\\033[0;31m" [9]="\\033[0;32m" [10]="\\033[0;33m" [11]="\\033[0;34m" [12]="\\033[0;35m" [13]="\\033[0;36m" [14]="\\033[0;37m" [15]="\\033[30;42m")

These Might Interest You

  • Remove ( color / special / escape / ANSI ) codes, from text, with sed Credit to the original folks who I've copied this command from. The diff here is: Theirs: [m|K] Theirs is supposed to remove \E[NUMBERS;NUMBERS[m OR K] This statement is incorrect in 2 ways. 1. The letters m and K are two of more than 20+ possible letters that can end these sequences. 2. Inside []'s , OR is already assumed, so they are also looking for sequences ending with | which is not correct. This : [a-zA-Z] This resolves the "OR" issue noted above, and takes care of all sequences, as they all end with a lower or upper cased letter. This ensures 100% of any escape code 'mess' is removed. Show Sample Output


    7
    sed "s,\x1B\[[0-9;]*[a-zA-Z],,g"
    Zhoul · 2013-03-18 14:49:30 1
  • Handles the color codes intended for 256-color terminals (such as xterm-(256)color and urxvt-unicode-256color), in addition to the standard 16-color ANSI forms. Overkill for strict ANSI output, see other options for something simpler.


    0
    sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
    rcbarnes · 2012-06-12 11:20:46 0
  • Removes ANSI color and end of line codes to the [{attr1};...;{attrn}m format.


    5
    sed -r "s/\x1B\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]//g
    vaejovis · 2009-11-03 00:34:06 1
  • The expression $(( $RANDOM * 6 / 32767 + 1 )) generates a random number between 1 and 6, which is then inserted into the escape sequence \e[3_m to switch the foreground color of the terminal to either red, green, yellow, blue, purple or cyan. The color can be reset using the escape sequence \e[0m. The full list of colors can be found here: https://wiki.archlinux.org/index.php/Color_Bash_Prompt#List_of_colors_for_prompt_and_Bash


    1
    echo -e "\e[3$(( $RANDOM * 6 / 32767 + 1 ))mHello World!"
    nst · 2013-07-28 13:01:12 0

What Others Think

how do you terminate the coloring so that subsequent shell prompts return to the original coloring?
bwoodacre · 456 weeks and 1 day ago
@bwoodacre: echo -e "\\033[0m]" will reset your formatting, I believe. Also, in terms that support it, "reset" would work
kaedenn · 456 weeks and 1 day ago
Erm, echo -e "\033[0m;", typos galore in my previous post
kaedenn · 456 weeks and 1 day ago
No need for seq for i in {0..7} or for ((i=0; i<=${#CC[@]} - 1; i++))
dennisw · 455 weeks and 2 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: