How to change the output color of echo in Linux

后端 未结 29 4031
刺人心
刺人心 2020-11-22 04:28

I am trying to print a text in the terminal using echo command.

I want to print the text in a red color. How can I do that?

29条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 04:53

    These codes work on my Ubuntu box:

    enter image description here

    echo -e "\x1B[31m foobar \x1B[0m"
    echo -e "\x1B[32m foobar \x1B[0m"
    echo -e "\x1B[96m foobar \x1B[0m"
    echo -e "\x1B[01;96m foobar \x1B[0m"
    echo -e "\x1B[01;95m foobar \x1B[0m"
    echo -e "\x1B[01;94m foobar \x1B[0m"
    echo -e "\x1B[01;93m foobar \x1B[0m"
    echo -e "\x1B[01;91m foobar \x1B[0m"
    echo -e "\x1B[01;90m foobar \x1B[0m"
    echo -e "\x1B[01;89m foobar \x1B[0m"
    echo -e "\x1B[01;36m foobar \x1B[0m"
    

    This prints the letters a b c d all in different colors:

    echo -e "\x1B[0;93m a \x1B[0m b \x1B[0;92m c \x1B[0;93m d \x1B[0;94m"
    

    For loop:

    for (( i = 0; i < 17; i++ )); 
    do echo "$(tput setaf $i)This is ($i) $(tput sgr0)"; 
    done
    

    enter image description here

提交回复
热议问题