Last active
November 22, 2023 12:35
-
-
Save YektaDev/d25f5bcd35d18fc47bfbf4e64a51299d to your computer and use it in GitHub Desktop.
C(olored) Echo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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