我正在尝试使用echo命令在终端中打印文本。
我想用红色打印文本。我该怎么做?
我正在尝试使用echo命令在终端中打印文本。
我想用红色打印文本。我该怎么做?
当前回答
echo -e "\033[31m Hello World"
[31m控制文本颜色:
30-37套前景色40-47套背景色
这里可以找到更完整的颜色代码列表。
最好将字符串末尾的文本颜色重置回\033[0m。
其他回答
其他的答案已经很好地解释了如何做到这一点。我仍然缺少的是对颜色代码的一个精心安排的概述。维基百科文章“ANSI转义码”对此非常有用。然而,由于颜色通常可以在每个终端中进行配置,并且看起来不同,所以我更希望有一个可以在终端中调用的功能。为此,我创建了以下函数来显示颜色表,并提醒我如何设置它们(排列方式受wiki文章的启发)。例如,您可以将它们加载到.bashrc/.zshrc中,或将它们作为脚本放在某处。
256色
由bash/zsh脚本生成:
function showcolors256() {
local row col blockrow blockcol red green blue
local showcolor=_showcolor256_${1:-bg}
local white="\033[1;37m"
local reset="\033[0m"
echo -e "Set foreground color: \\\\033[38;5;${white}NNN${reset}m"
echo -e "Set background color: \\\\033[48;5;${white}NNN${reset}m"
echo -e "Reset color & style: \\\\033[0m"
echo
echo 16 standard color codes:
for row in {0..1}; do
for col in {0..7}; do
$showcolor $(( row*8 + col )) $row
done
echo
done
echo
echo 6·6·6 RGB color codes:
for blockrow in {0..2}; do
for red in {0..5}; do
for blockcol in {0..1}; do
green=$(( blockrow*2 + blockcol ))
for blue in {0..5}; do
$showcolor $(( red*36 + green*6 + blue + 16 )) $green
done
echo -n " "
done
echo
done
echo
done
echo 24 grayscale color codes:
for row in {0..1}; do
for col in {0..11}; do
$showcolor $(( row*12 + col + 232 )) $row
done
echo
done
echo
}
function _showcolor256_fg() {
local code=$( printf %03d $1 )
echo -ne "\033[38;5;${code}m"
echo -nE " $code "
echo -ne "\033[0m"
}
function _showcolor256_bg() {
if (( $2 % 2 == 0 )); then
echo -ne "\033[1;37m"
else
echo -ne "\033[0;30m"
fi
local code=$( printf %03d $1 )
echo -ne "\033[48;5;${code}m"
echo -nE " $code "
echo -ne "\033[0m"
}
16种颜色
由bash/zsh脚本生成:
function showcolors16() {
_showcolor "\033[0;30m" "\033[1;30m" "\033[40m" "\033[100m"
_showcolor "\033[0;31m" "\033[1;31m" "\033[41m" "\033[101m"
_showcolor "\033[0;32m" "\033[1;32m" "\033[42m" "\033[102m"
_showcolor "\033[0;33m" "\033[1;33m" "\033[43m" "\033[103m"
_showcolor "\033[0;34m" "\033[1;34m" "\033[44m" "\033[104m"
_showcolor "\033[0;35m" "\033[1;35m" "\033[45m" "\033[105m"
_showcolor "\033[0;36m" "\033[1;36m" "\033[46m" "\033[106m"
_showcolor "\033[0;37m" "\033[1;37m" "\033[47m" "\033[107m"
}
function _showcolor() {
for code in $@; do
echo -ne "$code"
echo -nE " $code"
echo -ne " \033[0m "
done
echo
}
这是最简单、易读的解决方案。与bashj(https://sourceforge.net/projects/bashj/),您只需选择以下行之一:
#!/usr/bin/bash
W="Hello world!"
echo $W
R=130
G=60
B=190
echo u.colored($R,$G,$B,$W)
echo u.colored(255,127,0,$W)
echo u.red($W)
echo u.bold($W)
echo u.italic($W)
Y=u.yellow($W)
echo $Y
echo u.bold($Y)
如果您的终端应用程序支持颜色,则可以使用256x256x256色。
灵感来自@nachoparker的回答,我的.bashrc中有这个:
#### colours
source xcol.sh
### tput foreground
export tpfn=$'\e[0m' # normal
export tpfb=$(tput bold)
## normal colours
export tpf0=$(tput setaf 0) # black
export tpf1=$(tput setaf 1) # red
export tpf2=$(tput setaf 2) # green
export tpf3=$(tput setaf 3) # yellow
export tpf4=$(tput setaf 4) # blue
export tpf5=$(tput setaf 5) # magenta
export tpf6=$(tput setaf 6) # cyan
export tpf7=$(tput setaf 7) # white
# echo "${tpf0}black ${tpf1}red ${tpf2}green ${tpf3}yellow ${tpf4}blue ${tpf5}magenta ${tpf6}cyan ${tpf7}white${tpfn}"
## bold colours
export tpf0b="$tpfb$tpf0" # bold black
export tpf1b="$tpfb$tpf1" # bold red
export tpf2b="$tpfb$tpf2" # bold green
export tpf3b="$tpfb$tpf3" # bold yellow
export tpf4b="$tpfb$tpf4" # bold blue
export tpf5b="$tpfb$tpf5" # bold magenta
export tpf6b="$tpfb$tpf6" # bold cyan
export tpf7b="$tpfb$tpf7" # bold white
# echo "${tpf0b}black ${tpf1b}red ${tpf2b}green ${tpf3b}yellow ${tpf4b}blue ${tpf5b}magenta ${tpf6b}cyan ${tpf7b}white${tpfn}"
出口允许我使用这些tpf。。在Bash脚本中。
我刚刚将所有解决方案中的好捕获合并在一起,最终得出:
cecho(){
RED="\033[0;31m"
GREEN="\033[0;32m" # <-- [0 means not bold
YELLOW="\033[1;33m" # <-- [1 means bold
CYAN="\033[1;36m"
# ... Add more colors if you like
NC="\033[0m" # No Color
# printf "${(P)1}${2} ${NC}\n" # <-- zsh
printf "${!1}${2} ${NC}\n" # <-- bash
}
你可以称之为:
cecho "RED" "Helloworld"
我写了swag就是为了实现这一点。
你可以这样做
pip install swag
现在,您可以通过以下方式将所有转义命令作为txt文件安装到给定目标:
swag install -d <colorsdir>
或者更容易通过:
swag install
这将把颜色安装到~/.colors。
您可以这样使用它们:
echo $(cat ~/.colors/blue.txt) This will be blue
或者这样,我觉得更有趣:
swag print -c red -t underline "I will turn red and be underlined"
在ascinema上查看!