Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash colour one word using echo

Tags:

bash

echo

colors

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

like image 520
Zippyduda Avatar asked May 30 '13 20:05

Zippyduda


2 Answers

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

yellow='\E[1;33m'
like image 151
Lynn Avatar answered Sep 28 '22 14:09

Lynn


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}."
like image 30
Salah Eddine Taouririt Avatar answered Sep 28 '22 14:09

Salah Eddine Taouririt