I'm trying to apply a color into this printf trying to make it nice looking:
I'm building a script that shows a list of function names and their description of what it does.
#!/bin/bash
#Colours
greenColour="\e[0;32m\033[1m"
endColour="\033[0m\e[0m"
redColour="\e[0;31m\033[1m"
#Format the output in a table-like structure
printf "\n${greenColour}[ ]${endColour} %-15s %s\n" "${redColour}getPIP${endColour}" "Shows private IP"
But when I execute the script the first argument ([ ]) does get the colour applied but the second argument shows the color code instead of applying the other colour:
[ ] \e[0;31m\033[1mgetPIP\033[0m\e[0m Shows private IP
Any ideas if this is doable somehow?
Thanks in advance!
Expected: Apply colour to the second argument passed to printf Result: I see the colour code instead
CodePudding user response:
Double quotes are tricky in this case; a backslash doesn’t have the same meaning as in C’s double quotes… For example, this works for me:
greenColour=$'\033[01;32m'
endColour=$'\033[00m'
redColour=$'\033[01;31m'
printf "\n${greenColour}[ ]${endColour} %-15s %s\n" \
"${redColour}getPIP${endColour}" \
"Shows private IP"
On my terminal, [ ]
is green and getPIP
is red in this case. The codes used above are the “simplified” ones though, not the full 24-bit True Color ones.