Adding Colors To Bash Scripts

Forem Logo DEV Community Logo DEV Community Main Image

DEV Community

Follow

A space to discuss and keep up software development and manage your software career

Future Logo Future Main Image

Future

Follow

News and discussion of science and technology such as AI, VR, cryptocurrency, quantum computing, and more.

Open Forem Logo Open Forem Main Image

Open Forem

Follow

A general discussion space for the Forem community. If it doesn't have a home elsewhere, it belongs here

Gamers Forem Logo Gamers Forem Main Image

Gamers Forem

Follow

An inclusive community for gaming enthusiasts

Music Forem Logo Music Forem Main Image

Music Forem

Follow

From composing and gigging to gear, hot music takes, and everything in between.

Vibe Coding Forem Logo Vibe Coding Forem Main Image

Vibe Coding Forem

Follow

Discussing AI software development, and showing off what we're building.

Popcorn Movies and TV Logo Popcorn Movies and TV Main Image

Popcorn Movies and TV

Follow

Movie and TV enthusiasm, criticism and everything in-between.

DUMB DEV Community Logo DUMB DEV Community Main Image

DUMB DEV Community

Follow

Memes and software development shitposting

Design Community Logo Design Community Main Image

Design Community

Follow

Web design, graphic design and everything in-between

Security Forem Logo Security Forem Main Image

Security Forem

Follow

Your central hub for all things security. From ethical hacking and CTFs to GRC and career development, for beginners and pros alike

Golf Forem Logo Golf Forem Main Image

Golf Forem

Follow

A community of golfers and golfing enthusiasts

Crypto Forem Logo Crypto Forem Main Image

Crypto Forem

Follow

A collaborative community for all things Crypto—from Bitcoin to protocol development and DeFi to NFTs and market analysis.

Forem Core Logo Forem Core Main Image

Forem Core

Follow

Discussing the core forem open source software project — features, bugs, performance, self-hosting.

Parenting Logo Parenting Main Image

Parenting

Follow

A place for parents to the share the joys, challenges, and wisdom that come from raising kids. We're here for them and for each other.

Maker Forem Logo Maker Forem Main Image

Maker Forem

Follow

A community for makers, hobbyists, and professionals to discuss Arduino, Raspberry Pi, 3D printing, and much more.

HMPL.js Forem Logo HMPL.js Forem Main Image

HMPL.js Forem

Follow

For developers using HMPL.js to build fast, lightweight web apps. A space to share projects, ask questions, and discuss server-driven templating

Dropdown menu Dropdown menu Add reaction Like Unicorn Exploding Head Raised Hands Fire Jump to Comments Save Boost More... Copy link Copy link Copied to Clipboard Share to X Share to LinkedIn Share to Facebook Share to Mastodon Share Post via... Report Abuse

I recently wrote a post-receive git hook for a server and I wanted to easily distinguish my hook's output from git information. This led me to look into colorizing bash output.

By using ANSI color escape codes, we can add color to output strings. The ANSI standard specifies certain color codes;

Color Foreground Code Background Code
Black 30 40
Red 31 41
Green 32 42
Yellow 33 43
Blue 34 44
Magenta 35 45
Cyan 36 46
Light Gray 37 47
Gray 90 100
Light Red 91 101
Light Green 92 102
Light Yellow 93 103
Light Blue 94 104
Light Magenta 95 105
Light Cyan 96 106
White 97 107

To change the color of the text, what we want is the foreground code. There are also a few other non-color special codes that are relevant to us:

Code Description
0 Reset/Normal
1 Bold text
2 Faint text
3 Italics
4 Underlined text

The echo command prints out text. We need to tell it that we're working with special ANSI codes, not just regular characters. This can be accomplished by adding a \e at the beginning to form an escape sequence. The escape sequence for specifying color codes is \e[COLORm (COLOR represents our color code in this case). By default, echo does not support escape sequences. We need to add the -e option to enable their interpretation.

To print red text, therefore, we could have

echo -e "\e[32mRed text\e[0m" Enter fullscreen mode Exit fullscreen mode

The \e[0m means we use the special code 0 to reset text color back to normal. Without this, all other text you print out after this would be red.

This works, but it would be more readable if we store the color codes in variables and use those instead.

RED="\e[31m" ENDCOLOR="\e[0m" echo -e "${RED}Red text${ENDCOLOR}" Enter fullscreen mode Exit fullscreen mode

Putting all these together, we could have a script like this

#! /usr/bin/env bash RED="\e[31m" GREEN="\e[32m" ENDCOLOR="\e[0m" echo -e "${RED}This is some red text, ${ENDCOLOR}" echo -e "${GREEN}And this is some green text${ENDCOLOR}" Enter fullscreen mode Exit fullscreen mode

Demoing this

We can combine escape codes to get more fancy output.

#! /usr/bin/env bash RED="31" GREEN="32" BOLDGREEN="\e[1;${GREEN}m" ITALICRED="\e[3;${RED}m" ENDCOLOR="\e[0m" echo -e "${BOLDGREEN}Behold! Bold, green text.${ENDCOLOR}" echo -e "${ITALICRED}Italian italics${ENDCOLOR}" Enter fullscreen mode Exit fullscreen mode

Another one

You can use these in any number of ways to make your scripts less monotonous. The combinations are up to you. Happy scripting!

pic Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Submit Preview Dismiss Collapse Expand moopet profile image Ben Sinclair Ben Sinclair Ben Sinclair Follow I've been a professional C, Perl, PHP and Python developer. I'm an ex-sysadmin from the late 20th century. These days I do more Javascript and CSS and whatnot, and promote UX and accessibility.
  • Location Scotland
  • Education Something something cybernetics
  • Pronouns They/them
  • Work General-purpose software person
  • Joined Aug 29, 2017
Nov 23 '19 Dropdown menu
  • Copy link
  • Hide

Using escape codes directly is cool, but I'm a fan of using tput like this:

# SET Attribute Foreground <colour 123> kindalightblue=$(tput setaf 123) Enter fullscreen mode Exit fullscreen mode

There are a lot of handy things tput can do, and though the capabilities vary with your terminal, and though it's not completely portable either, you get all the colours your terminal can show.

Collapse Expand ifenna__ profile image Ifenna Ifenna Ifenna Follow Nov 23 '19 • Edited on Nov 23 • Edited Dropdown menu
  • Copy link
  • Hide

I haven't heard about tput before. I'll definitely check it out.

EDIT: Just took a look. It's great. Thanks for the tip 🚀.

Collapse Expand jianwu profile image jianwu jianwu jianwu Follow
  • Joined Jun 30, 2019
Jul 27 '21 Dropdown menu
  • Copy link
  • Hide

Thanks for the great article. However in my environment, if I run the example, it will print the escape code literally. e.g.

$ echo -e "\e[32mRed text\e[0m" \e[32mRed text\e[0m Enter fullscreen mode Exit fullscreen mode

For me the solution is to use printf, e.g.

echo -e $(printf "\e[32mRed text\e[0m")` Enter fullscreen mode Exit fullscreen mode Collapse Expand toransahu profile image Toran Sahu Toran Sahu Toran Sahu Follow An OSS Enthusiast
  • Email [email protected]
  • Location India
  • Education National Institute of Technology, Raipur
  • Work Senior Software Engineer at Talentica Softwares
  • Joined Jan 17, 2021
Oct 17 '22 Dropdown menu
  • Copy link
  • Hide

Try $ echo -e "\033[32mRed text\e[0m" \e[32mRed text\033[0m. i.e. \033 instead \e.

Collapse Expand absoftware profile image Ariel Bogdziewicz Ariel Bogdziewicz Ariel Bogdziewicz Follow iOS apps and PHP/MySQL backend services. Familiar also with C/C++, C#, web development and Linux systems.
  • Location Kristiansand, Norway
  • Education Wroclaw University of Technology, Institute of Information Technology, Automatics and Robotics
  • Work Lead Software Developer at Telia Norge AS
  • Joined Sep 3, 2020
Jul 7 '22 Dropdown menu
  • Copy link
  • Hide

This helped me as well in macOS. Thank you!

Collapse Expand sombody101 profile image Sombody101 Sombody101 Sombody101 Follow
  • Joined Aug 27, 2022
Aug 27 '22 Dropdown menu
  • Copy link
  • Hide

Try using -ne rather than -e.

Collapse Expand keithy profile image Keith Keith Keith Follow
  • Joined Jun 8, 2019
Mar 24 '23 • Edited on Mar 24 • Edited Dropdown menu
  • Copy link
  • Hide
bgreen () { printf "\e[1m\e[32m" ; $@ ; printf "\e[0m"; } red () { printf "\e[31m" ; $@ ; printf "\e[0m"; } bgreen echo "Am I green?" Enter fullscreen mode Exit fullscreen mode Collapse Expand laakkus profile image laakkus laakkus laakkus Follow
  • Joined Mar 15, 2024
Mar 15 '24 • Edited on Mar 15 • Edited Dropdown menu
  • Copy link
  • Hide

I have RED already in use, so had to do this:

NORMAL=0 BOLD=1 FAINT=2 ITALIC=3 UNDERLINE=4 RED='\033[0;31m' ITALICRED=$(echo $RED|sed -e "s/\[./\[${ITALIC}/") FAINTRED=$(echo $RED|sed -e "s/\[./\[${FAINT}/") Enter fullscreen mode Exit fullscreen mode Collapse Expand placidrod profile image Placid Rodrigues Placid Rodrigues Placid Rodrigues Follow Software Engineer
  • Location Auckland, NZ
  • Work Software Engineer at Movio
  • Joined Feb 26, 2019
May 22 '20 Dropdown menu
  • Copy link
  • Hide

Thank you for the super helpful post. I think the first example should be: echo -e "\e[31mRed text\e[0m" 32 represents green.

Collapse Expand johnnyxbell profile image Johnny Bell Johnny Bell Johnny Bell Follow G’day mates, I’m Johnny 👋🏻 Frontend Engineer, a11y advocate, & speaker.
  • Location Orange County, CA
  • Work Software Engineer at Weedmaos
  • Joined Dec 7, 2018
Nov 23 '19 Dropdown menu
  • Copy link
  • Hide

Great article. Something that should be simple but is actually super hard to understand thank you

Collapse Expand ifenna__ profile image Ifenna Ifenna Ifenna Follow Nov 23 '19 Dropdown menu
  • Copy link
  • Hide

Glad to help.

Collapse Expand dtmilano profile image Diego Torres Milano Diego Torres Milano Diego Torres Milano Follow
  • Joined Nov 26, 2019
Nov 26 '19 Dropdown menu
  • Copy link
  • Hide

If you want to see the colors you can use this script: gist.github.com/dtmilano/4055d6df5... which uses tput.

Collapse Expand kubeden profile image kuberdenis kuberdenis kuberdenis Follow Denis, Denis, Kuberdenis
  • Work Software Consultant / DevOps
  • Joined Jul 1, 2020
Nov 30 '21 Dropdown menu
  • Copy link
  • Hide

Great post!

Collapse Expand ifenna__ profile image Ifenna Ifenna Ifenna Follow Feb 8 '20 Dropdown menu
  • Copy link
  • Hide

Thanks for noticing! Fixed it.

Collapse Expand kognianov profile image Kliment Ognianov Kliment Ognianov Kliment Ognianov Follow IT & eCommerce architect, full stack developer, Magento 2 certified solutions specialist, IoT enthusiast, scientist.
  • Location Sofia, Bulgaria
  • Joined Sep 12, 2019
Feb 16 '24 Dropdown menu
  • Copy link
  • Hide

Just add '5' as a blinking special, non-colour code ;)

View full discussion (15 comments) Code of Conduct Report abuse

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.

Hide child comments as well

Confirm

For further actions, you may consider blocking this person and/or reporting abuse

Ifenna Follow
  • Work Software Engineer at Meld Gold
  • Joined May 31, 2018
Paige Bailey profile image Where we're going, we don't need chatbots: introducing the Antigravity IDE 🚀 #programming #ai #productivity #webdev Anthony Max profile image 9 Developer Tools That Will Boost Your Workflow in 2026 🔥 #webdev #programming #productivity #ai Sylwia Laskowska profile image 7 Lies Developers Keep Telling Themselves (And Somehow Still Believe) #jokes #discuss
DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

Tag » Colours Sh Script