Hello,
I am working on a Bash script and I wanted to change the color of some of the output to emphasize on some specific words.
For example, I want to be able to print the text in green when the script is successful and in red when the script fails.
Does anyone know an easy way of doing so?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Hi there,
Yes, you can do that with the ANSI escape characters. You can find a list of those characters here.
For example, if you wanted to print green text, you could do the following:
A quick rundown of the script:
clear
so that we could reset the color of the terminal, otherwise all of the output will be green.printf
we printout the messageHere is the output of the script:
If you prefer using
echo
instead ofprintf
you need to make sure that you add the-e
flag:Here are some other common colors that you could:
Output:
You can also change the text background with the following:
Output:
Hope that this helps! Regards, Bobby