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脚本编写小工具。在性能方面,它与显式地放入转义代码相比如何?