11

我想在回声句子中间为一个单词着色,但似乎无法实现这一点。

这有效:

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

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

4

2 回答 2

19

更好的是,使用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}."
于 2013-05-30T22:22:04.823 回答
7

m您在 ANSI 转义码中忘记了yellow. 这有效:

yellow='\E[1;33m'
于 2013-05-30T21:03:59.603 回答