BASH Function to Error Warnings and Messages with Terminal Colours

in programming •  2 months ago 

We sometimes need in BASH script to display messages with different format/colour to terminal console. For example, the warnings messages are blue, and error messages are red. We can use the following customize echo functions to display messages of different types. The colours are defined and easily customizable.

#!/bin/bash

terminalColorClear='\033[0m'
terminalColorEmphasis='\033[1;32m'
terminalColorError='\033[1;31m'
terminalColorMessage='\033[1;33m'
terminalColorWarning='\033[1;34m'

echoDefault() {
    echo -e "${terminalColorClear}$1${terminalColorClear}"
}

echoMessage() {
    echo -e "${terminalColorMessage}$1${terminalColorClear}"
}

echoWarning() {
    echo -e "${terminalColorWarning}$1${terminalColorClear}"
}

echoError() {
    echo -e "${terminalColorError}$1${terminalColorClear}"
}

echoError "echoError"
echoWarning "echoWarning"
echoMessage "echoMessage"
echoDefault "echoDefault"

This will show different messages to console:

--EOF (The Ultimate Computing & Technology Blog) --

Reposted to Blog

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Thank you for reading ^^^^^^^^^^^^^^^

NEW! Following my Trail (Upvote or/and Downvote)

Follow me for topics of Algorithms, Blockchain and Cloud.
I am @justyy - a Steem Witness
https://steemyy.com

My contributions

Delegation Service

Important Update of Delegation Service!

  • Delegate 1000 to justyy: Link
  • Delegate 5000 to justyy: Link
  • Delegate 10000 to justyy: Link

Support me

If you like my work, please:

  1. Delegate SP: https://steemyy.com/sp-delegate-form/?delegatee=justyy
  2. Vote @justyy as Witness: https://steemyy.com/witness-voting/?witness=justyy&action=approve
  3. Set @justyy as Proxy: https://steemyy.com/witness-voting/?witness=justyy&action=proxy
    Alternatively, you can vote witness or set proxy here: https://steemit.com/~witnesses

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order: