I am wanting to colourize one word in the middle of an echo sentence, but can't seem to achieve this.
This works:
#!/bin/bash
wipe="\033[1m\033[0m"
yellow='\E[1;33'
echo -e "$yellow"
echo Hello World
echo -e "$wipe"
But this doesn't:
#!/bin/bash
wipe="\033[1m\033[0m"
yellow='\E[1;33'
black="40m"
echo -e "Output a $yellow coloured $wipe word."
# or
echo -e "Output a ${yellow} coloured ${wipe} word."
What am I stupidly doing wrong? :)
You forgot an m
in your ANSI escape code for yellow
. This works:
yellow='\E[1;33m'
Much better, use tput to set a foreground colour:
textreset=$(tput sgr0) # reset the foreground colour
red=$(tput setaf 1)
yellow=$(tput setaf 2)
echo "Output a ${yellow} coloured ${textreset} ${red} word ${textreset}."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With