Coloured BASH Output Using TPUT

From my previous post, found here, you can see that I have formatted the text to be a specific colour depending on what sort of output I get. So for errors I make the text red and for successful messages I make the text green.

This is easy to implement into BASH scripts and a lot of other formatting can be applied as well. In this post, I will be covering the colorization (probably not a word), underlining, bold text and resetting the changes.

Colour Possibilities:

  • 0 – Black
  • 1 – Red
  • 2 – Green
  • 3 – Yellow
  • 4 – Blue
  • 5 – Magenta
  • 6 – Cyan
  • 7 – White
$(tput setaf 1)TEXT HERE

Bold Text:

#Starts bold characters

$(tput bold)TEXT HERE

#Ends bold characters

$(tput sgr0)TEXT HERE

I would just like to add here that tput sgr0 removes all formatting and returns text to the default style and colour.

Underlining:

#Starts underlining

$(tput smul)TEXT HERE

#Ends underlining

$(tput rmul)TEXT HERE

Below is a full script which includes all the possible combinations of the examples above, apart from black text.

#!/bin/bash

echo "
regular bold underline
$(tput setaf 1)Text $(tput bold)Text $(tput sgr0)$(tput setaf 1)$(tput smul)Text$(tput rmul)
$(tput setaf 2)Text $(tput bold)Text $(tput sgr0)$(tput setaf 2)$(tput smul)Text$(tput rmul)
$(tput setaf 3)Text $(tput bold)Text $(tput sgr0)$(tput setaf 3)$(tput smul)Text$(tput rmul)
$(tput setaf 4)Text $(tput bold)Text $(tput sgr0)$(tput setaf 4)$(tput smul)Text$(tput rmul)
$(tput setaf 5)Text $(tput bold)Text $(tput sgr0)$(tput setaf 5)$(tput smul)Text$(tput rmul)
$(tput setaf 6)Text $(tput bold)Text $(tput sgr0)$(tput setaf 6)$(tput smul)Text$(tput rmul)
$(tput setaf 7)Text $(tput bold)Text $(tput sgr0)$(tput setaf 7)$(tput smul)Text$(tput rmul)
"

I know it looks quite horrible in the source code but this is what the output looks like:

Full possibilities

Enjoy!

Leave a Comment

Your email address will not be published. Required fields are marked *

email popup image
Mark Harwood
NEVER miss a blog post again! Subscribe for email notifications whenever a new post is live!
Subscribe
NEVER miss a blog post again! Subscribe for email notifications whenever a new post is live!
Fill Out This Form, And I Will Be In Touch Shortly
Contact form image
I'll Be In Touch Soon