What color codes can I use in my Bash PS1 prompt?

I used several colors in my bash PS1 prompt such as:

33]01;31] # pink
33]00m]   # white
33]01;36] # bold green
33]02;36] # green
33]01;34] # blue
33]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.

See also: How can I get my PS1 prompt to show time, user, host, directories, and Git branch

Asked By: Michael Durrant

||

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

Answered By: Michael Durrant

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:

33]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:

xterm  256 color chart

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 chart1 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="[33[01;32m]MyPrompt: [33[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).

Also note that if you want to include the output of a command run every time the prompt is used (as opposed to just once when the prompt is set), you should set it as a literal string with single quotes, e.g.:

PS1='[33[01;32m]$(date): [33[0m]'

Although this is not a great example if you are happy with using bash’s special d or D{format} prompt escapes — which are not the topic of the question but can be found in man bash under PROMPTING. There are various other useful escapes such as w for current directory, u for current user, etc.


1. The main portion of this chart, colors 16 – 231 (notice they are not in numerical order) are a 6 x 6 x 6 RGB color cube. “Color cube” refers to the fact that an RGB color space can be represented using a three dimensional array (with one axis for red, one for green, and one for blue). Each color in the cube here can be represented as coordinates in a 6 x 6 x 6 array, and the index in the chart calculated thusly:

    16 + R * 36 + G * 6 + B

The first color in the cube, at index 16 in the chart, is black (RGB 0, 0, 0). You could use this formula in shell script:

#!/bin/sh                                                         

function RGBcolor {                                               
    echo "16 + $1 * 36 + $2 * 6 + $3" | bc                        
}                                                                 

fg=$(RGBcolor 1 0 2)  # Violet                                            
bg=$(RGBcolor 5 3 0)  # Bright orange.                                            

echo -e "\033[1;38;5;$fg;48;5;${bg}mviolet on tangerine\033[0m"
Answered By: goldilocks

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 "ne[1;4mAscii Escape Code Helper Utilitye[mnn"
  printf "  e[1mUseage:e[m colors.sh [-|-b|-f|-bq|-fq|-?|?] [start] [end] [step]nn"
  printf "The values for the first parameter may be one of the following:nn"
  printf "  e[1m-e[m  Will result in the default output.n"
  printf "  e[1m-be[m This will display the 8 color version of this chart.n"
  printf "  e[1m-fe[m This will display the 256 color version of this chart using foreground colors.n"
  printf "  e[1m-qe[m This will display the 256 color version of this chart without the extra text.n"
  printf "  e[1m-bqe[m    This will display the 8 color version of this chart without the extra text.n"
  printf "  e[1m-fqe[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,fqe[mnn"
  printf "  e[1mstarte[m  The color index to begin display at.n"
  printf "  e[1mende[m    The color index to stop display at.n"
  printf "  e[1mstarte[m  The number of indexes to increment color by each iteration.nnn"

}
verbose() {
  if [[ "$1" != "-q" && "$1" != "-fq" && "$1" != "-bq" ]]; then
    printf "nTo control the display style use e[1m%se[m where e[1m%se[m is:n" 'e[{$value}[:{$value}]m' '{$value}'
    printf "n  0 Normal e[1m1 Bolde[m e[2m2 Dime[m e[3m3 ???e[m e[4m4 Underlinede[m e[5m5 Blinke[m e[6m6 ???e[m e[7m7 Invertede[m e[8m8 Hiddene[mnn"
    printf "If e[1m%se[m is not provided it will reset the display.nn" '{$value}'
  fi
}
eight_color() {
    local fgc bgc vals seq0
    if [ "$1" != "-bq" ]; then
        printf "ne[1;4m8 Color Escape Value Pallettee[mnn"
        printf "Color escapes are e[1m%se[mn" 'e[${value};...;${value}m'
        printf "    Values e[1m30..37e[m are e[1mforegrounde[m colorsn"
        printf "    Values e[1m40..47e[m are e[1mbackgrounde[m colorsnn"  
    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}TEXTe[m"
            printf " e[${vals:+${vals+$vals;}}1mBOLDe[m"
        done
        printf "e[0mn"
    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 "ne[1;4m256 Color Escape Value Pallettee[0mnn"
    printf "    e[1m%se[m for e[1mbackgrounde[m colorsn    e[1m%se[m for e[1mforegrounde[m colorsnn" '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[mn"
    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.

Answered By: krowe

I wrote a bash function that can show you all the colors, if this helps.

function colorgrid( )
{
    iter=16
    while [ $iter -lt 52 ]
    do
        second=$[$iter+36]
        third=$[$second+36]
        four=$[$third+36]
        five=$[$four+36]
        six=$[$five+36]
        seven=$[$six+36]
        if [ $seven -gt 250 ];then seven=$[$seven-251]; fi

        echo -en "33[38;5;$(echo $iter)m█ "
        printf "%03d" $iter
        echo -en "   33[38;5;$(echo $second)m█ "
        printf "%03d" $second
        echo -en "   33[38;5;$(echo $third)m█ "
        printf "%03d" $third
        echo -en "   33[38;5;$(echo $four)m█ "
        printf "%03d" $four
        echo -en "   33[38;5;$(echo $five)m█ "
        printf "%03d" $five
        echo -en "   33[38;5;$(echo $six)m█ "
        printf "%03d" $six
        echo -en "   33[38;5;$(echo $seven)m█ "
        printf "%03d" $seven

        iter=$[$iter+1]
        printf 'rn'
    done
}

You can throw that in a .bashrc / .bash_profile / .bash_aliases or save it as a script and run it that way.
You can use the colors to change color like I did with my name below.

colorgrid() outputs:
Output of colorgrid()

I changed my name in my .bash_profile by doing this:

if [ "$USER" = "plasmarob" ]; then
    p="[33[01;38;5;52m]p"
    l="[33[01;38;5;124m]l"
    a="[33[01;38;5;196m]a"
    s="[33[01;38;5;202m]s"
    m="[33[01;38;5;208m]m"
    a2="[33[01;38;5;214m]a"
    r="[33[01;38;5;220m]r"
    o="[33[01;38;5;226m]o"
    b="[33[01;38;5;228m]b"
    local __user_and_host="$p$l$a$s$m$a2$r$o$b"
else
    local __user_and_host="[33[01;36m]u"
fi   

...

export PS1="$__user_and_host $__cur_location $__git_branch_color$__git_branch$__prompt_tail$__last_color "

Note that the 01 prefix in a string like [33[01;38;5;214m]a
sets it to be bold.

Answered By: Plasmarob

You could build upon @goldilocks and @Michael Durrant’s answers to create something more readable like this:

CYAN="[e[01;36m]"
WHITE="[e[01;37m]"
BLUE="[e[01;34m]"
TEXT_RESET="[e[00m]"   
TIME="t"
CURRENT_PATH="W"
ROOT_OR_NOT="$"

export PS1="${CYAN}[${BLUE}${TIME}${WHITE} ${CURRENT_PATH}${CYAN}]${ROOT_OR_NOT}${TEXT_RESET} "

Which will result in:

preview

Answered By: Philippe Fanaro
cat "$0" 1>&2;
#
# = 256-color test =
#
# [
# |*| Source: https://unix.stackexchange.com/a/643715
# |*| Source (original): https://misc.flogisoft.com/bash/tip_colors_and_formatting#colors2
# |*| Last update: CE 2021-05-15 03:17 UTC ]
#
#
# This script shall echo a bunch of color codes generating a fancy color table: demonstrating the 256-color compatibility of the shell / terminal.
#
#
#
#
# == Implementation ==
#

# === Table 0..15 ===

    Colors="$(
# Colors (0..15):
    i=0;

    while
    echo "$i";
    [ $i -lt 15 ];

    do
    i=$(( $i + 1 ));

    done;
    )";


    echo;

    for x0 in 
    '48' '38'; # Background / Foreground

    do {
    for Color in 
    $Colors;

    do {
    printf 'e['"$x0"';5;%sm  %3s  ' 
    "$Color" "$Color"; # [Note 1]

# 8 entries per line:
    [ $(( ($Color + 1) % 8 )) -eq 0 ] && echo -e 'e[m'; # [Note 1]
    };

    done;
    };

    done;

    unset Colors x0;
    echo;


# === Table 16..255 ===

    for Color in 
    $(
# Colors (16..255):
    i=16;

    while
    echo "$i";
    [ $i -lt 255 ];

    do
    i=$(( $i + 1 ));

    done;
    );


    do {
    Colors="$Colors $Color";

# 6 entries per group:
    [ $(( ($Color + 1) % 6 )) -eq 4 ] && {
    for x0 in 
    '38' '48'; # Foreground / Background

    do {
    for Color in 
    $Colors;

    do
    printf 'e['"$x0"';5;%sm  %3s  ' 
    "$Color" "$Color"; # [Note 1]

    done;

    echo -ne 'e[m'; # [Note 1]
    };

    done;

    unset Colors x0;
    echo;
    };

    };


    done;

    unset Color;
    echo;
#
#
#
#
# == Notes ==
#
# [Note 1]
# [
# For explanation on the color code:
# |*| Coloring test utility: https://unix.stackexchange.com/a/643536 ]
#
cat "$0" 1>&2;
#
# = 256-color test (old) =
#
# [
# |*| Source: https://unix.stackexchange.com/a/643715
# |*| Source (original): https://misc.flogisoft.com/bash/tip_colors_and_formatting#colors2
# |*| Last update: CE 2021-05-15 03:17 UTC ]
#
#
# Basically a replicate of the original with no logic change. Left there mostly for reference.
#
#
#
#
# == Implementation ==
#
# Colors (0..255):
    Colors="$(
    i=0;

    while
    echo "$i";
    [ $i -lt 255 ];

    do
    i=$(( $i + 1 ));

    done;
    )";


    echo;

    for x0 in 
    '38' '48'; # Foreground / Background

    do {
    for Color in 
    $Colors;

    do {
    printf 'e['"$x0"';5;%sm  %3s  ' 
    "$Color" "$Color";

# 6 entries per line:
    [ $(( ($Color + 1) % 6 )) -eq 4 ] && echo -e 'e[m';
    };

    done;

    echo;
    };

    done;

    unset Colors x0 Color;
Answered By: Stock Exchange

Just another 8-bit 256-color helper script

It provides:

  • environment variables

    • for all 4-bit default colors (but as 8-bit code).
    • for escape and end codes
  • functions:

    • 8-bit colorgrid (by plasmarob)
    • a 4-bit colornames grid displaying colors and backrounds with the color names (I have to admit the variables have a prefix which is not displayed. I thought about removing the prefixes later.)
    • colorhelp prints a colorful helptext to remind the user how to use alls this.

Installation:

I store the script to /etc/profile.d/bash_colors.sh so that the functions and color-variables are available system-wide.

To install it just for one user, store it to ~/.bash_colors and add following code to your ~/.bash_aliases file (which itself should be loaded within ~/.bashrc):

if [ -f ~/.bash_colors ]; then
    . ~/.bash_colors 
fi

Screenshots

colorhelp

colorhelp

colorgrid

Also invokes colorhelp (again, credits to plasmarob for the grid):
colorgrid (also invokes colorhelp)

colornames

Note that the 4-bit black isn’t necessarily pitch black. This is because the client software decides how to interpret the 4-bit colors. That means by configuring your client, you can adjust the color palette for the codes in the range of 0-15 completely.
colornames

bash_colors.sh

#!/bin/bash

# Adds some color helpers.
# 8-bit 256-color lookup table
# 2022, Justus Kenklies
#
# credits:
#    based on colorgrid function by plasmarob:
#    https://unix.stackexchange.com/questions/124407/what-color-codes-can-i-use-in-my-bash-ps1-prompt/285956#285956
#


# Escape codes
ESC_SEQ="33["
COL_RESET=$ESC_SEQ"0m"

FG=$ESC_SEQ"38;5;"
BG=$ESC_SEQ"48;5;"

# standard colors
BLACK=$FG"0m"
RED=$FG"1m"
GREEN=$FG"2m"
YELLOW=$FG"3m"
BLUE=$FG"4m"
MAGENTA=$FG"5m"
CYAN=$FG"6m"
WHITE=$FG"7m"

# high intensity colors
BRIGHT_BLACK=$FG"8m"; GRAY=$BRIGHT_BLACK; GREY=$GRAY
BRIGHT_RED=$FG"9m"
BRIGHT_GREEN=$FG"10m"
BRIGHT_YELLOW=$FG"11m"
BRIGHT_BLUE=$FG"12m"
BRIGHT_MAGENTA=$FG"13m"
BRIGHT_CYAN=$FG"14m"
BRIGHT_WHITE=$FG"15m"


# background standard colors
BG_BLACK=$BG"0m"
BG_RED=$BG"1m"
BG_GREEN=$BG"2m"
BG_YELLOW=$BG"3m"
BG_BLUE=$BG"4m"
BG_MAGENTA=$BG"5m"
BG_CYAN=$BG"6m"
BG_WHITE=$BG"7m"

# background high intensity colors
BG_BRIGHT_BLACK=$BG"8m"; BG_GRAY=$BG_BRIGHT_BLACK; BG_GREY=$BG_GRAY
BG_BRIGHT_RED=$BG"9m"
BG_BRIGHT_GREEN=$BG"10m"
BG_BRIGHT_YELLOW=$BG"11m"
BG_BRIGHT_BLUE=$BG"12m"
BG_BRIGHT_MAGENTA=$BG"13m"
BG_BRIGHT_CYAN=$BG"14m"
BG_BRIGHT_WHITE=$BG"15m"


function printcolor() {
        local FG=$1
        local BG=$2

        fg=`eval echo "${$FG}"`
        bg=`eval echo "${BG_$BG}"`
        eval echo -en "$fg$bg"

        printf ' $%-17s' $FG;
        echo -en "${COL_RESET}"
}

#lower backgrounds
function colornames() {
        local colors=(BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITE)
        local lowup=()
        lowup[0]="_"
        lowup[1]="_BRIGHT_"

        local fgcolor bgcolor
        echo "List of color variables. "
        #table
        for bgbright in ${lowup[@]}
        do
                #echo "writing ${bgbright:1} bg colors"
                #column header
                for bg in ${colors[@]}
                do
                        # echo -en "$$bgt"
                        printf ' $BG_%-14s' ${bgbright:1}$bg;
                done

                # line feed:
                echo ""

                for fgbright in ${lowup[@]}
                do

                        #echo "writing ${fgbright:1} fg colors"
                        for fg in ${colors[@]}
                        do
                                fgcolor=${fgbright:1}$fg

                                for bg in ${colors[@]}
                                do
                                        bgcolor=${bgbright:1}$bg
                                        printcolor $fgcolor $bgcolor
                                done

                                # line feed:
                                echo ""
                        done
                done
                echo ""
        done
}

function colorhelp() {
        echo -e "nTo write colored text, either begin with ${RED}${ESC_SEQ}${COL_RESET} (${MAGENTA}\33[${COL_RESET}) and"
        echo -e "add a color code from ${MAGENTA}colorgrid()${COL_RESET}, "
        echo -e "or begin with ${RED}${(RED|GREEN|YELLOW|BLUE|MAGENTA|CYAN)}${COL_RESET}."
        echo -e "Close the text with $RED${COL_RESET}${COL_RESET} (${MAGENTA}\33[0m${COL_RESET})"
        echo -en "nExample:n${YELLOW}echo $RED-e $YELLOW"$RED"
        echo -en "Look: ${MAGENTA}${ESC_SEQ}${BLUE}38;5;243m${RED}This is dark grey text${MAGENTA}${COL_RESET}${RED} and this is normal text."
        echo -e "$YELLOW"$COL_RESET"

        echo -e "Look: ${ESC_SEQ}38;5;243mThis is dark gray text${COL_RESET} and this is normal text.n"
}

function colorgrid() {
        iter=16
        while [ $iter -lt 52 ]; do
                second=$[$iter+36]
                third=$[$second+36]
                four=$[$third+36]
                five=$[$four+36]
                six=$[$five+36]
                seven=$[$six+36]
                if [ $seven -gt 250 ];then seven=$[$seven-251]; fi

                echo -en "33[38;5;$(echo $iter)m█ "
                printf "%03d" $iter
                echo -en "   33[38;5;$(echo $second)m█ "
                printf "%03d" $second
                echo -en "   33[38;5;$(echo $third)m█ "
                printf "%03d" $third
                echo -en "   33[38;5;$(echo $four)m█ "
                printf "%03d" $four
                echo -en "   33[38;5;$(echo $five)m█ "
                printf "%03d" $five
                echo -en "   33[38;5;$(echo $six)m█ "
                printf "%03d" $six
                echo -en "   33[38;5;$(echo $seven)m█ "
                printf "%03d" $seven


                iter=$[$iter+1]
                printf 'rn'
        done
        echo -e "$COL_RESET"
        echo "Example for color 153:"
        echo -e "echo -e "33[38;5;153m\33[38;5;153mHello World\33[0m33[0m""
        colorhelp
}

Answered By: Justus Kenklies

RGB mnemonics can be used instead of color sequence: e[s;38;2;r;g;bm

Where s is style, and r,g,b are decimal numbers in the range of 0-255.

R='[e[38;2;255;100;100m]'                                                                                                            
G='[e[38;2;100;255;100m]'
B='[e[38;2;100;100;255m]'
W='[e[0m]'
PS1="[$Ru$W@$Bh$W:$Gw$W]$ "

enter image description here

#!/bin/bash
function colorgrid()
{
    end=250
    for((red=0; red <= end; red+=75)); do
        for((green=0; green <= end; green+=75)); do
            for style in 0 "1;3"; do
                for((blue=0; blue <= end; blue+=5)); do
                    printf "e[$style;38;2;$red;$green;${blue}mH"
                done
                printf "e[0mn"
            done
        done
    done
}

enter image description here

Answered By: user1602
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.