How To Change The Color Of The Output In Linux Bash? - DigitalOcean

  • Blog
  • Docs
  • Get Support
  • Contact Sales
  • Tutorials
  • Questions
  • Product Docs
  • Cloud Chats
  • Search Community

Report this

What is the reason for this report?This undefined is spamThis undefined is offensiveThis undefined is off-topicThis undefined is otherSubmit<- Back to questionsQuestionHow to change the color of the output in Linux Bash?Posted on January 11, 2021Linux BasicsLinux CommandsCentOSUbuntubitmap

By bitmap

Popular topics

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?

Submit answer (1)Add a comment (0)

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!

Sign In or Sign Up to AnswerThese 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.0BobbyBobbyJanuary 11, 2021

Accepted Answer

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:

#!/bin/bash # Set the color variable green='\033[0;32m' # Clear the color after that clear='\033[0m' printf "The script was executed ${green}successfully${clear}!"

A quick rundown of the script:

  • First we set the green color as a variable
  • After that we set a variable called clear so that we could reset the color of the terminal, otherwise all of the output will be green.
  • Then using printf we printout the message

Here is the output of the script:

Linux Bash output colors

If you prefer using echo instead of printf you need to make sure that you add the -e flag:

#!/bin/bash # Set the color variable green='\033[0;32m' # Clear the color after that clear='\033[0m' echo -e "The script was executed ${green}successfully${clear}!"

Here are some other common colors that you could:

#!/bin/bash # Color variables red='\033[0;31m' green='\033[0;32m' yellow='\033[0;33m' blue='\033[0;34m' magenta='\033[0;35m' cyan='\033[0;36m' # Clear the color after that clear='\033[0m' # Examples echo -e "The color is: ${red}red${clear}!" echo -e "The color is: ${green}green${clear}!" echo -e "The color is: ${yellow}yellow${clear}!" echo -e "The color is: ${blue}blue${clear}!" echo -e "The color is: ${magenta}magenta${clear}!" echo -e "The color is: ${cyan}cyan${clear}!"

Output:

Bash text color on output

You can also change the text background with the following:

#!/bin/bash # Color variables bg_red='\033[0;41m' bg_green='\033[0;42m' bg_yellow='\033[0;43m' bg_blue='\033[0;44m' bg_magenta='\033[0;45m' bg_cyan='\033[0;46m' # Examples echo -e "The background color is: ${red}red${clear}!" echo -e "The background color is: ${green}green${clear}!" echo -e "The background color is: ${yellow}yellow${clear}!" echo -e "The background color is: ${blue}blue${clear}!" echo -e "The background color is: ${magenta}magenta${clear}!" echo -e "The background color is: ${cyan}cyan${clear}!"

Output: Bash background color for output text

Hope that this helps! Regards, Bobby

Reply

Popular Topics

  1. AI/ML
  2. Ubuntu
  3. Linux Basics
  4. JavaScript
  5. Python
  6. MySQL
  7. Docker
  8. Kubernetes
  9. All tutorials
  10. Talk to an expert
  • All tutorials
  • All topic tags

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Sign Up

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Learn more

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Learn more

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

SubmitSubmit

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

View all products

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

Get started

*This promotional offer applies to new accounts only.

© 2026 DigitalOcean, LLC.Sitemap.

Tag » Colours Sh Script