Bash颜色一个字使用echo

Zippyduda 10 bash colors echo

我想在一个回声句中间对一个词进行着色,但似乎无法实现这一点.

这有效:

#!/bin/bash
wipe="\033[1m\033[0m"
yellow='\E[1;33'
echo -e "$yellow"
echo Hello World
echo -e "$wipe"

但这不是:

#!/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."

我愚蠢的做错了什么?:)

tarrsalah.. 17

更好的是,使用tput设置前景色:

textreset=$(tput sgr0) # reset the foreground colour
red=$(tput setaf 1)
yellow=$(tput setaf 2) 

echo "Output a ${yellow} coloured ${textreset} ${red} word ${textreset}."


Lynn.. 6

您忘记了mANSI转义码中的yellow.这有效:

yellow='\E[1;33m'