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.
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:
clearso that we could reset the color of the terminal, otherwise all of the output will be green.
printfwe printout the message
Here is the output of the script:
If you prefer using
printfyou need to make sure that you add the
Here are some other common colors that you could:
You can also change the text background with the following:
Hope that this helps! Regards, Bobby