Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I used several colors in my PS1 prompt such as

\033]01;31\] # pink
\033]00m\]   # white
\033]01;36\] # bold green
\033]02;36\] # green
\033]01;34\] # blue
\033]01;33\] # bold yellow

Where can I find a list of the color codes I can use?

I looked at Colorize Bash Console Color but it didn't answer my question about a list of the actual codes.

It would be nice if there was a more readable form also.

share|improve this question
1  
Note that the final \] here is actually not part of the color sequence; it serves a purpose in setting prompts specifically (I've added a few paragraphs to the end of my answer about this). "It would be nice if there was a more readable form also." -> the cut n' paste in your own answer is one way to do this. –  goldilocks Apr 14 at 17:27
    
+1 that is useful info. thanks! –  Michael Durrant Apr 14 at 20:54

3 Answers 3

up vote 9 down vote accepted

Those are ANSI escape sequences; that link is to a chart of color codes but there are other interesting things on that wikipedia page as well. Not all of them work on (e.g.) a normal linux console.

This is incorrect:

\033]00m\] # white

0 resets the terminal to its default (which is probably white). The actual code for white foreground is 37. Also, the escaped closing brace at the end (\]) is not part of the color sequence (see the last few paragraphs below for an explanation of their purpose in setting a prompt).

Note that some GUI terminals allow you to specify a customized color scheme. This will affect the output.

There's a list here which adds 7 foreground and 7 background colors I had not seen before, but they seem to work:

# Foreground colors
90   Dark gray  
91   Light red  
92   Light green    
93   Light yellow   
94   Light blue 
95   Light magenta  
96   Light cyan  

# Background colors
100  Dark gray  
101  Light red  
102  Light green    
103  Light yellow   
104  Light blue 
105  Light magenta  
106  Light cyan 

In addition, if you have a 256 color GUI terminal (I think most of them are now), you can apply colors from this chart:

enter image description here

The ANSI sequence to select these, using the number in the bottom left corner, starts 38;5; for the foreground and 48;5; for the background, then the color number, so e.g.:

echo -e "\\033[48;5;95;38;5;214mhello world\\033[0m"

Gives me a light orange on tan (meaning, the color chart is roughly approximated).

You can see the colors in this chart as they would appear on your terminal fairly easily:

#!/bin/bash

color=16;

while [ $color -lt 245 ]; do
    echo -e "$color: \\033[38;5;${color}mhello\\033[48;5;${color}mworld\\033[0m"
    ((color++));
done  

The output is self-explanatory.

Some systems set the $TERM variable to xterm-256color if you are on a 256 color terminal via some shell code in /etc/profile. On others, you should be able to configure your terminal to use this. That will let TUI applications know there are 256 colors, and allow you to add something like this to your ~/.bashrc:

if [[ "$TERM" =~ 256color ]]; then
     PS1="MyCrazyPrompt..."
fi

Beware that when you use color escape sequences in your prompt, you should enclose them in escaped (\ prefixed) square brackets, like this:

PS1="\[\033[01;32m\]MyPrompt: \[\033[0m\]"

Notice the ['s interior to the color sequence are not escaped, but the enclosing ones are. The purpose of the latter is to indicate to the shell that the enclosed sequence does not count toward the character length of the prompt. If that count is wrong, weird things will happen when you scroll back through the history, e.g., if it is too long, the excess length of the last scrolled string will appear attached to your prompt and you won't be able to backspace into it (it's ignored the same way the prompt is).

share|improve this answer
    
I suggest for original asker to test color availability with a test chart. There is one here: robmeerman.co.uk/unix/… or it can be very easy to do one, if one does not trust shell scripts found on internet. –  IBr Apr 12 at 16:52
1  
@IBr Interesting point. Just viewing all the colors is a drop dead simple task, so I few lines of bash above to do this. –  goldilocks Apr 12 at 17:05
    
The color reference script found here might be more useful, being compact yet still having the codes and with each color separated for clarity. –  Michael Plotke May 1 at 20:27
    
Please, don't use echo for anything other than literal text that doesn't start with a dash (-). It's unportable. All common implementations violate the standard which states that no options should be supported. Worse, they're inconsistent. You should use printf instead. (And do not embed variables inside printf statements, use %s.) –  nyuszika7h May 4 at 15:08
    
colortest-256 list the xterm pallette in a nice compact form. (apt-get install colortest if missing) –  Volker Siegel Jul 3 at 3:13

Another script like the one posted by TAFKA 'goldilocks' for displaying colors which is maybe a little more practical for reference purposes:

#!/bin/bash

useage() {
  printf "\n\e[1;4mAscii Escape Code Helper Utility\e[m\n\n"
  printf "  \e[1mUseage:\e[m colors.sh [-|-b|-f|-bq|-fq|-?|?] [start] [end] [step]\n\n"
  printf "The values for the first parameter may be one of the following:\n\n"
  printf "  \e[1m-\e[m  Will result in the default output.\n"
  printf "  \e[1m-b\e[m This will display the 8 color version of this chart.\n"
  printf "  \e[1m-f\e[m This will display the 256 color version of this chart using foreground colors.\n"
  printf "  \e[1m-q\e[m This will display the 256 color version of this chart without the extra text.\n"
  printf "  \e[1m-bq\e[m    This will display the 8 color version of this chart without the extra text.\n"
  printf "  \e[1m-fq\e[m    This will display the 256 color version of this chart using foreground colors without the extra text.\n"
  printf "  \e[1m-?|?\e[m   Displays this help screen.\n"
  printf "\nThe remaining parameters are only used if the first parameter is one of: \e[1m-,-f,q,fq\e[m\n\n"
  printf "  \e[1mstart\e[m  The color index to begin display at.\n"
  printf "  \e[1mend\e[m    The color index to stop display at.\n"
  printf "  \e[1mstart\e[m  The number of indexes to increment color by each iteration.\n\n\n"

}
verbose() {
  if [[ "$1" != "-q" && "$1" != "-fq" && "$1" != "-bq" ]]; then
    printf "\nTo control the display style use \e[1m%s\e[m where \e[1m%s\e[m is:\n" '\e[{$value}[:{$value}]m' '{$value}'
    printf "\n  0 Normal \e[1m1 Bold\e[m \e[2m2 Dim\e[m \e[3m3 ???\e[m \e[4m4 Underlined\e[m \e[5m5 Blink\e[m \e[6m6 ???\e[m \e[7m7 Inverted\e[m \e[8m8 Hidden\e[m\n\n"
    printf "If \e[1m%s\e[m is not provided it will reset the display.\n\n" '{$value}'
  fi
}
eight_color() {
    local fgc bgc vals seq0
    if [ "$1" != "-bq" ]; then
        printf "\n\e[1;4m8 Color Escape Value Pallette\e[m\n\n"
        printf "Color escapes are \e[1m%s\e[m\n" '\e[${value};...;${value}m'
        printf "    Values \e[1m30..37\e[m are \e[1mforeground\e[m colors\n"
        printf "    Values \e[1m40..47\e[m are \e[1mbackground\e[m colors\n\n"  
    fi
    for fgc in {30..37}; do
        for bgc in {40..47}; do
            fgc=${fgc#37}
            bgc=${bgc#40}
            vals="${fgc:+$fgc;}${bgc}"
            vals=${vals%%;}
            seq0="${vals:+\e[${vals}m}"
            printf "  %-9s" "${seq0:-(default)}"
            printf " ${seq0}TEXT\e[m"
            printf " \e[${vals:+${vals+$vals;}}1mBOLD\e[m"
        done
        printf "\e[0m\n"
    done
}


if [[ "$1" == "-b" ||  "$1" == "-bq" ]]; then
  eight_color "$1"
  verbose "$1"
elif [[ "$1" == "" || "$1" == "-" ||  "$1" == "-f" ||  "$1" == "-q" ||  "$1" == "-fq" ]]; then
  start=${2:-0}
  end=${3:-255}
  step=${4:-1}
  color=$start
  style="48;5;"
  if [[ "$1" == "-f" || "$1" == "-fq" ]]; then
   style="38;5;"
  fi
  perLine=$(( ( $(tput cols) - 2 ) / 9 ));
  if [[ "$1" != "-q" && "$1" != "-fq" ]]; then
    printf "\n\e[1;4m256 Color Escape Value Pallette\e[0m\n\n"
    printf "    \e[1m%s\e[m for \e[1mbackground\e[m colors\n    \e[1m%s\e[m for \e[1mforeground\e[m colors\n\n" '\e[48;5;${value}m' '\e[38;5;${value}m'
  fi
  while [ $color -le $end ]; do
    printf "\e[m \e[${style}${color}m  %3d  \e[m " $color
    ((color+=step))
    if [ $(( ( ( $color - $start ) / $step ) % $perLine )) -eq 0 ]; then
      printf "\n"
    fi
    done
    printf "\e[m\n"
    verbose "$1"
else
  useage
fi

This should size correctly for the terminal you are using. It is a little over the top for this purpose but now you can control many aspects of how this displays via parameters. Hopefully, they are all self explanatory.

share|improve this answer
    
No kill like overkill :) –  Navin Apr 13 at 5:04

Looks like at least some of the list is:

txtblk='\e[0;30m' # Black - Regular
txtred='\e[0;31m' # Red
txtgrn='\e[0;32m' # Green
txtylw='\e[0;33m' # Yellow
txtblu='\e[0;34m' # Blue
txtpur='\e[0;35m' # Purple
txtcyn='\e[0;36m' # Cyan
txtwht='\e[0;37m' # White
bldblk='\e[1;30m' # Black - Bold
bldred='\e[1;31m' # Red
bldgrn='\e[1;32m' # Green
bldylw='\e[1;33m' # Yellow
bldblu='\e[1;34m' # Blue
bldpur='\e[1;35m' # Purple
bldcyn='\e[1;36m' # Cyan
bldwht='\e[1;37m' # White
unkblk='\e[4;30m' # Black - Underline
undred='\e[4;31m' # Red
undgrn='\e[4;32m' # Green
undylw='\e[4;33m' # Yellow
undblu='\e[4;34m' # Blue
undpur='\e[4;35m' # Purple
undcyn='\e[4;36m' # Cyan
undwht='\e[4;37m' # White
bakblk='\e[40m'   # Black - Background
bakred='\e[41m'   # Red
bakgrn='\e[42m'   # Green
bakylw='\e[43m'   # Yellow
bakblu='\e[44m'   # Blue
bakpur='\e[45m'   # Purple
bakcyn='\e[46m'   # Cyan
bakwht='\e[47m'   # White
txtrst='\e[0m'    # Text Reset

based on https://wiki.archlinux.org/index.php/Color_Bash_Prompt

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.