How to display colour text in terminal?
6,545
You may try with this command:
echo -e "\033[1;31m This is red text"
31 is the "color" and 1 is the "style".
You can play with different color:
for i in {30..37}; do echo -e "\033[1;$i""m colorful text\033[0m"; done
And different styles:
for i in {1..7}; do echo -e "\033[$i;31""m different style\033[0m"; done
Notice:
The "\033[0m" at the end of the string is like a closing tag, so that it won't affect the text after.
Author by
Arin
Hello World!!! I am a software developer new to the IT industry. Want to learn about Linux, Big-Data, Cloud technology. Like to participate open source projects and want to involve more in this huge community of excellent developers :)
Updated on September 18, 2022Comments
-
Arin over 1 year
I want to display colour text in terminal(bash shell). Tried with this approach:
echo -e "\e[1;31m This is red text \e[0m"
But it doesn't change the text colour to red.
-
Fanatique over 5 yearsWhat is the terminal you're using it in? It might be that your terminal doesn't support colors. That command outputs red text to me
-
Arin over 5 years@Fanatique Default terminal of mac. Doesn't it support text colouring?
-
-
Arin over 5 yearsYou are talking about changing the terminal colour/style. But the requirement is to display coloured text using shell-script.
-
Arin over 5 yearsIn my system Display ANSI colors is already enabled. However, Text colour doesn't change after
export CLICOLOR=1
to .bash_profile. The reference that you have shared talks about changing the colour of PS1 which is working as expected. @Fanatique -
Arin over 5 yearsThanks, @Yoric. This approach perfectly works for me.
-
JdeBP over 5 yearsThis answer does not really make clear the cause of the problem, which is explained at unix.stackexchange.com/a/461073/5132 and is everything to do with the quirks of the
echo
command.