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 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.

share|improve this question
1  
Add 1; in it: \e[1;38;05;${code}m. Better to use tput setaf "$code"; tput bold. –  Stéphane Chazelas Oct 2 '13 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 Oct 2 '13 at 10:27

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer

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.

share|improve this answer
    
Some escape characters seems to get lost during the copy-paste. –  manatwork Oct 2 '13 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]}). –  Gilles Oct 2 '13 at 21:48

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.