Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
BASH: set variables for ANSI text color escape sequences
RESTORE=$(echo -en '\033[0m')
RED=$(echo -en '\033[00;31m')
GREEN=$(echo -en '\033[00;32m')
YELLOW=$(echo -en '\033[00;33m')
BLUE=$(echo -en '\033[00;34m')
MAGENTA=$(echo -en '\033[00;35m')
PURPLE=$(echo -en '\033[00;35m')
CYAN=$(echo -en '\033[00;36m')
LIGHTGRAY=$(echo -en '\033[00;37m')
LRED=$(echo -en '\033[01;31m')
LGREEN=$(echo -en '\033[01;32m')
LYELLOW=$(echo -en '\033[01;33m')
LBLUE=$(echo -en '\033[01;34m')
LMAGENTA=$(echo -en '\033[01;35m')
LPURPLE=$(echo -en '\033[01;35m')
LCYAN=$(echo -en '\033[01;36m')
WHITE=$(echo -en '\033[01;37m')
# Test
Copy link

ionescu77 commented May 1, 2015

Hi, thank you alot! This is just great!

Found you here: StackOverflow

Copy link

adilsoncarvalho commented Feb 20, 2017

kudos 👏

Copy link

mathieu-aubin commented Feb 12, 2018

Hi, for what its worth, i have created a nice little function that shows the many different colors that can be used in the terminal and its available as part of a little centos7 base installation setup i created a while back.

The function code as of today is as follows:

# Displays useful sets of terminal colors based on param input either set by default aliases or user-defined... When looking
# for a color theme for a project, bash-colors-random has prooven to effortlessly throw some decent color combination.
function bash-colors() {
  local SEQNUM=${1:-4}; [[ "$SEQNUM" -eq "0" ]] && SEQNUM=4;
  tput rmam;
  seq -ws ' ' 0 ${SEQNUM} 256 | \xargs -n1 bash -c \
  'echo -ne "\033[1;48;5;${0}m \\\033[48;5;${0}m \033[0m"; \
  echo -ne "\033[1;7;38;5;${0}m\\\033[7;38;5;${0}m \033[0m"; \
  echo -ne " \033[1;38;5;${0}m\\\033[1;38;5;${0}m\033[0m"; \
  echo -ne " \033[38;5;${0}m\\\033[38;5;${0}m\033[0m"; \
  echo -ne " \033[2;38;5;${0}m\\\033[2;38;5;${0}m\033[0m"; \
  echo -ne " \033[3;38;5;${0}m\\\033[3;38;5;${0}m\033[0m"; \
  echo -ne " \033[4;38;5;${0}m\\\033[4;38;5;${0}m\033[0m"; \
  echo -ne " \033[9;38;5;${0}m\\\033[9;38;5;${0}m\033[0m"; \
  echo -ne " \033[4;9;38;5;${0}m\\\033[4;9;38;5;${0}m\033[0m"; \
  echo -e " \033[1;3;4;9;38;5;${0}m\\\033[1;3;4;9;38;5;${0}m\033[0m"';
  tput smam;
alias bash-colors-full='bash-colors 1';
alias bash-colors-minimal='bash-colors 8';
alias bash-colors-less='bash-colors 2';
alias bash-colors-random='bash-colors $(shuf -n1 -i 1-64)';

and is available here:

In order to add this function to your set of 'tools' you can either create a separate .bash_functions file that you later source from your main rc file or you can use this code:

curl -sLk >> ~/.bashrc

Then source the ~/.bashrc file using source ~/.bashrc or opening another terminal. You will then have 'bash-colors' (and its derivates) available for your pleasure.

Here's a little demo (thanks to asciinema - click on the pic to view)


The i have removed the \033[ from the output in the demo function to shorten the output the function remains the same.


Copy link

codemedic commented Apr 3, 2019

Thanks @mathieu-aubin ... thats a great little utility.

I have created that as a gist, with some bashism tweaks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment