Image of the glider from the Game of Life by John Conway
Skip to content

Add Colors To Your ZSH Scripts

I was writing some scripts this morning to help me keep the Unix and Linux server I administer at work up to date with their NTP time synchronization. As I was going along, I thought to myself, "I'd like to see some color in the output." Thankfully, I already had the code in my ZSH prompt. All I needed to do was remove some sigils, and I was up and running. If you want to add color to the output of your ZSH scripts, here's what you need to add:

1
2
3
4
5
6
7
8
9
autoload colors
if [[ "$terminfo[colors]" -gt 8 ]]; then
    colors
fi
for COLOR in RED GREEN YELLOW BLUE MAGENTA CYAN BLACK WHITE; do
    eval $COLOR='$fg_no_bold[${(L)COLOR}]'
    eval BOLD_$COLOR='$fg_bold[${(L)COLOR}]'
done
eval RESET='$reset_color'

You now have the following variables available in the shell script namepace: RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, BLACK, WHITE, BOLD_RED, BOLD_GREEN, BOLD_YELLOW, BOLD_BLUE, BOLD_MAGENTA, BOLD_CYAN, BOLD_BLACK, BOLD_WHITE, RESET. Using these variables, you can manipulate the output from "echo" and "printf" for your script. For example, here's a screenshot using "echo" to print red, green and blue text to the screen. Notice that I'm using the "RESET" variable after the blue text to reset my prompt text back to normal. This may or may not be necessary, depending on how you configured your prompt, but it's not a bad habit to get into.

Thought this might be helpful to the larger scripting community or for those sysadmins, such as myself, who would like a little variety added to their script output.

{ 2 } Comments

  1. Yorokobi using Firefox 3.5.6 on Ubuntu | December 18, 2009 at 8:55 pm | Permalink

    ### Add color to grep
    export GREP_COLOR="1;4;32"
    alias grep='egrep --color=auto'

    ### Colorize less
    export LESS_TERMCAP_mb=$'\E[01;36m'
    export LESS_TERMCAP_md=$'\E[01;36m'
    export LESS_TERMCAP_me=$'\E[0m'
    export LESS_TERMCAP_se=$'\E[0m'
    export LESS_TERMCAP_so=$'\E[01;44;33m'
    export LESS_TERMCAP_ue=$'\E[0m'
    export LESS_TERMCAP_us=$'\E[04;32m'

  2. ylluminate using Safari 534.45 on Mac OS | June 16, 2011 at 11:04 pm | Permalink

    Thanks for the idea here. Some good stuff you've shared for ZSH. Curious, I'm seeing an issue where the escape sequence for the ASCII color is emitting before a first echo test, example:
    $ echo ${RED}RED
    22;31mREDRED

    Appears that the echo is echoing itself and then the actual colored text. I'm assuming I've got my terminal set up incorrectly, or perhaps it's an anomaly with Mac OS X Terminal. Any thoughts?

{ 2 } Trackbacks

  1. [...] Add Colors To Your ZSH Scripts [...]

  2. [...] Add Colors To Your ZSH Scripts December 18th 2009 7:07pm Linux. GNU. Freedom. From: pthree.org [...]

    [WORDPRESS HASHCASH] The comment's server IP (208.113.210.27) doesn't match the comment's URL host IP (74.54.185.210) and so is spam.

Post a Comment

Your email is never published nor shared.

Switch to our mobile site