Skip to content

Instantly share code, notes, and snippets.

@YektaDev
Last active November 22, 2023 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YektaDev/d25f5bcd35d18fc47bfbf4e64a51299d to your computer and use it in GitHub Desktop.
Save YektaDev/d25f5bcd35d18fc47bfbf4e64a51299d to your computer and use it in GitHub Desktop.
C(olored) Echo
#!/bin/zsh
#
# cecho - A script to echo colored text using tput
# Author: Ali Khaleqi Yekta
#
# Ensure the inputs are provided.
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Usage: cecho <color> <text>"
exit 1
fi
color_name="$1"
text=("${@[2,-1]}")
# Map color names to tput setaf color codes.
case "$color_name" in
"black") color_code=0;;
"red") color_code=1;;
"green") color_code=2;;
"yellow") color_code=3;;
"blue") color_code=4;;
"magenta") color_code=5;;
"cyan") color_code=6;;
"white") color_code=7;;
*) echo "Invalid color name. Available colors: black, red, green, yellow, blue, magenta, cyan, white"; exit 1;;
esac
tput bold; tput setaf $color_code; echo $text; tput sgr0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment