Skip to content

Instantly share code, notes, and snippets.

@amberj
Last active October 2, 2024 19:04
Show Gist options
  • Save amberj/5166112 to your computer and use it in GitHub Desktop.
Save amberj/5166112 to your computer and use it in GitHub Desktop.
A bash pretty print script that provides following red/green/blue colored echo functions.
#!/bin/bash
#
## @file rgb-colored-echo.sh
## @author Amber Jain
## @section DESCRIPTION A bash pretty print script which provides red/green/blue colored echo functions
## @section LICENSE ISC
#################
# Documentation #
#################
#
# A bash pretty print script which provides following red/green/blue colored echo functions:
# red_echo
# simple_red_echo
# green_echo
# simple_green_echo
# blue_echo
# simple_blue_echo
#
# Simple copy/paste function definitions or 'source' this script in your bash script and then you can use these functions.
##########################
# Start of script 'body' #
##########################
SELF_NAME=$(basename $0)
# Prints warning/error $MESSAGE in red foreground color
#
# For e.g. You can use the convention of using RED color for [E]rror messages
red_echo() {
echo -e "\x1b[1;31m[E] $SELF_NAME: $MESSAGE\e[0m"
}
simple_red_echo() {
echo -e "\x1b[1;31m$MESSAGE\e[0m"
}
# Prints success/info $MESSAGE in green foreground color
#
# For e.g. You can use the convention of using GREEN color for [S]uccess messages
green_echo() {
echo -e "\x1b[1;32m[S] $SELF_NAME: $MESSAGE\e[0m"
}
simple_green_echo() {
echo -e "\x1b[1;32m$MESSAGE\e[0m"
}
# Prints $MESSAGE in blue foreground color
#
# For e.g. You can use the convetion of using BLUE color for [I]nfo messages
# that require special user attention (especially when script requires input from user to continue)
blue_echo() {
echo -e "\x1b[1;34m[I] $SELF_NAME: $MESSAGE\e[0m"
}
simple_blue_echo() {
echo -e "\x1b[1;34m$MESSAGE\e[0m"
}
#########
# Usage #
#########
echo "This is a 'normal' echo!"
echo
MESSAGE="This is a RED colored message!" ; red_echo
MESSAGE="This is a simple RED colored message!" ; simple_red_echo
echo
MESSAGE="This is a GREEN colored message!" ; green_echo
MESSAGE="This is a simple GREEN colored message!" ; simple_green_echo
echo
MESSAGE="This is a BLUE colored message!" ; blue_echo
MESSAGE="This is a simple BLUE colored message!" ; simple_blue_echo
#################
# End of script #
#################
@deny-7
Copy link

deny-7 commented Feb 28, 2024

Try something like this

red_echo() {
    echo -e "\x1b[1;31m $1\e[0m"
}

green_echo() {
    echo -e "\x1b[1;32m $1\e[0m"
}

green_echo "Some description"

Seems like author does not use ascii colours, and this is the reason why your terminal emulator shows plain codes instead of colors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment