BASH Function to Error Warnings and Messages with Terminal Colours


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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/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"
#!/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:

echo-color-terminal BASH Function to Error Warnings and Messages with Terminal Colours bash script

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
189 words
Last Post: Teaching Kids Programming - Delete Node in a Linked List (No access to Head)
Next Post: Teaching Kids Programming - Square Matrix Diagonal Sum

The Permanent URL is: BASH Function to Error Warnings and Messages with Terminal Colours

Leave a Reply