Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 用回音给一个单词涂颜色 我想在回音句中间加上一个词,但似乎不能达到这个目的。_Bash_Colors_Echo - Fatal编程技术网

Bash 用回音给一个单词涂颜色 我想在回音句中间加上一个词,但似乎不能达到这个目的。

Bash 用回音给一个单词涂颜色 我想在回音句中间加上一个词,但似乎不能达到这个目的。,bash,colors,echo,Bash,Colors,Echo,这项工作: #!/bin/bash wipe="\033[1m\033[0m" yellow='\E[1;33' echo -e "$yellow" echo Hello World echo -e "$wipe" yellow='\E[1;33m' 但这并不是: #!/bin/bash wipe="\033[1m\033[0m" yellow='\E[1;33' black="40m" echo -e "Output a $yellow coloured $wipe word." # or

这项工作:

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

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

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

您忘记了黄色的ANSI转义码中的
m
。这项工作:

#!/bin/bash
wipe="\033[1m\033[0m"
yellow='\E[1;33'
echo -e "$yellow"
echo Hello World
echo -e "$wipe"
yellow='\E[1;33m'
更好的方法是,使用设置前景色:

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

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

我将尝试一下tput,只是最近才看到,看起来不错。谢谢。还有用于
tput
的。这是一个非常棒的shell脚本编写小工具。在性能方面,它与显式地放入转义代码相比如何?