Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bash echo in color
# ----------------------------------
# Colors
# ----------------------------------
NOCOLOR='\033[0m'
RED='\033[0;31m'
GREEN='\033[0;32m'
ORANGE='\033[0;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
LIGHTGRAY='\033[0;37m'
DARKGRAY='\033[1;30m'
LIGHTRED='\033[1;31m'
LIGHTGREEN='\033[1;32m'
YELLOW='\033[1;33m'
LIGHTBLUE='\033[1;34m'
LIGHTPURPLE='\033[1;35m'
LIGHTCYAN='\033[1;36m'
WHITE='\033[1;37m'
alias whichpill='echo -e "You take the ${BLUE}blue pill${NOCOLOR}—the story ends, you wake up in bed and believe whatever you want to believe. You take the ${RED}red pill${NOCOLOR}—you stay in Wonderland, and I show you how deep the rabbit hole goes. Remember: all I’m offering is the ${YELLOW}truth${NOCOLOR}. Nothing more."';
@jonsuh

This comment has been minimized.

Copy link
Owner Author

@jonsuh jonsuh commented Feb 4, 2016

Put the above in your Bash user config (commonly stored in ~/.bash_profile or ~/.bashrc on OS X). Then restart Terminal or reload your config source ~/.bash_profile

Run whichpill and you should see the following:

When echo-ing, use the -e option so it properly interprets the backslashed-escaped characters of the colors, like such:

echo -e "The first five colors of the rainbow are ${RED}red ${ORANGE}orange ${YELLOW}yellow ${GREEN}green ${NOCOLOR}and ${BLUE}blue${NOCOLOR}"

Remember to end the string you’re echo-ing with ${NOCOLOR}, otherwise the last color will persist, like this:

@Jerry-Lau

This comment has been minimized.

Copy link

@Jerry-Lau Jerry-Lau commented Feb 7, 2020

RED ORANGE YELLOW GREEN BLUE 红橙黄绿蓝靛紫

@Jerry-Lau

This comment has been minimized.

Copy link

@Jerry-Lau Jerry-Lau commented Feb 7, 2020

end the string with nocolor

@jessuppi

This comment has been minimized.

Copy link

@jessuppi jessuppi commented Aug 21, 2020

Beautiful colors and easy to follow, thanks! We borrowed this for SlickStack and credited your Gist:

https://github.com/littlebizzy/slickstack

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.