13

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? :)

Zippyduda
  • 107
  • 2
  • 7
  • 19

2 Answers2

20

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}."
Salah Eddine Taouririt
  • 24,925
  • 20
  • 60
  • 96
8

You forgot an m in your ANSI escape code for yellow. This works:

yellow='\E[1;33m'
Lynn
  • 10,425
  • 43
  • 75