Sunday, May 5, 2024
 Popular · Latest · Hot · Upcoming
11
rated 0 times [  11] [ 0]  / answers: 1 / hits: 4001  / 2 Years ago, sat, may 14, 2022, 12:03:31

I'm a bit new to bash scripting, and I'm wondering if there is a program or built-in command to pipe to that will print in a specified color? Or is there an echo argument to do so?



Like I could do:



echo Hi | commandhere -arguement blue


and it would print "Hi" in the color blue?


More From » bash

 Answers
0

I don't know of any utility for colored printing itself, but you can do it easily with a shell function like this:



# colorize stdin according to parameter passed (GREEN, CYAN, BLUE, YELLOW)
colorize(){
GREEN="033[0;32m"
CYAN="033[0;36m"
GRAY="033[0;37m"
BLUE="033[0;34m"
YELLOW="033[0;33m"
NORMAL="033[m"
color=$${1:-NORMAL}
# activate color passed as argument
echo -ne "`eval echo ${color}`"
# read stdin (pipe) and print from it:
cat
# Note: if instead of reading from the pipe, you wanted to print
# the additional parameters of the function, you could do:
# shift; echo $*
# back to normal (no color)
echo -ne "${NORMAL}"
}
echo hi | colorize GREEN


If you want to check other colors, take a look at this list.
You can add support for any color from there, simply creating an additional variable at this function with the correct name and value.


[#38541] Saturday, May 14, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brellked

Total Points: 63
Total Questions: 107
Total Answers: 104

Location: Morocco
Member since Fri, May 22, 2020
4 Years ago
;