5

I want to list all colors that I can use in bash console. And after it I want to set my prompt to be bold and orange like color. I'm using this to do listing color codes for me:

for code in {0..255}; do echo -e "\e[38;05;${code}m $code: Test"; done

But the question is how to make it bold and keep colors?

I looked for some advice about making it bold here: http://misc.flogisoft.com/bash/tip_colors_and_formatting but I couldn't found any code that combines bold and one of 256 colors.

3
  • 3
    Add 1; in it: \e[1;38;05;${code}m. Better to use tput setaf "$code"; tput bold. Commented Oct 2, 2013 at 10:16
  • @StephaneChazelas Wow I didn't know I could add another code inside sequence. I was certain whet there could be max three parts. For example: \e[38;05;44m or \e[1;31m. Does the order of codes between semicolons matter? Your answer is correct. Please write reply and I will accept it. Thx.
    – piotrekkr
    Commented Oct 2, 2013 at 10:27
  • misc.flogisoft.com/bash/tip_colors_and_formatting
    – slm
    Commented Sep 19, 2018 at 1:57

2 Answers 2

4

You can write it as any of those:

echo -e "\e[1;38;05;${code}m $code: Test"
echo -e "\e[1m\e[38;05;${code}m $code: Test";
echo -e "\e[38;05;${code}m\e[1m $code: Test";
echo -e "\e[38;05;${code};1m $code: Test";
tput bold; tput setaf "$code" # provided the terminfo database is
                              # properly populated

You can run tput bold only once provided you don't reset the boldness with a tput sgr0 or \e[m or \e[0m.

3

To supplement Stephane's example, here's a quick shell function to list all 256 colors:

#!/bin/bash
esc=$'\033'
for row in {0..15} ; 
do
    rowtext=
    for col in {0..15}; 
    do
        color=$(( $row * 16 + $col))
        BG="${esc}[48;5;${color}m"
        rowtext=${rowtext}$BG\ 
        if [[ $color -lt 100 ]]; then rowtext=${rowtext}$BG\   ;fi 
        if [[ $color -lt 10 ]]; then rowtext=${rowtext}$BG\   ;fi 
        rowtext=${rowtext}$BG${color}
        rowtext=${rowtext}$BG\ 
    done
    echo "${rowtext}${esc}[00m "
done

This is a quick "port" of a zsh function I have to bash. I seems to work (in bash), or at least well enough.

The color code number is displayed in the color block. This is what you'd use in your prompt or elsewhere to set the color.

2
  • Some escape characters seems to get lost during the copy-paste.
    – manatwork
    Commented Oct 2, 2013 at 14:07
  • An escape character at the beginning of BG got lost. You should use escape sequences instead to avoid this kind of mishap. You can't use $BG[0] in bash, you have to write it ${BG:0:1} (for an array it would be ${array[0]}). Commented Oct 2, 2013 at 21:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .