Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Im having the following issue:

On bash I set:

COLOR="0;31" #red color

If I do

echo -e "\e[00;31mRED\e[00m" 

I get

RED <- in red color on the terminal

However if I do

echo -e "\e[0$COLORm \RED\e[00m"

I just get

RED <- without colors

How can I print using color and my $COLOR variable?

share|improve this question

3 Answers 3

bash does not know anything about variable COLORm. But following example will work

COLOR="0;31m"
echo -e "\e[0$COLOR \RED\e[0m"
share|improve this answer

Use ${var} – e.g. \e[0;${COLOR}m – to separate the variable name from the surrounding text.

Alternatively, use printf "\e[0;%sm %s \e[m\n" "$COLOR" "RED"

(Side note: \e[00;31m is not necessary; \e[0;31m or just \e[;31m is fine. Same applies to \e[00m)

share|improve this answer

Use curly brackets to isolate the variable name and eliminate ambiguity in expressions where the variable names are adjecent to some literal characters (such as ${hello}world) or other tricky places:

enter image description here

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.