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:
if [[ "$terminfo[colors]" -gt 8 ]]; then
for COLOR in RED GREEN YELLOW BLUE MAGENTA CYAN BLACK WHITE; do
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.