我正在尝试使用echo命令在终端中打印文本。
我想用红色打印文本。我该怎么做?
我正在尝试使用echo命令在终端中打印文本。
我想用红色打印文本。我该怎么做?
当前回答
感谢@k-five的回答
declare -A colors
#curl www.bunlongheng.com/code/colors.png
# Reset
colors[Color_Off]='\033[0m' # Text Reset
# Regular Colors
colors[Black]='\033[0;30m' # Black
colors[Red]='\033[0;31m' # Red
colors[Green]='\033[0;32m' # Green
colors[Yellow]='\033[0;33m' # Yellow
colors[Blue]='\033[0;34m' # Blue
colors[Purple]='\033[0;35m' # Purple
colors[Cyan]='\033[0;36m' # Cyan
colors[White]='\033[0;37m' # White
# Bold
colors[BBlack]='\033[1;30m' # Black
colors[BRed]='\033[1;31m' # Red
colors[BGreen]='\033[1;32m' # Green
colors[BYellow]='\033[1;33m' # Yellow
colors[BBlue]='\033[1;34m' # Blue
colors[BPurple]='\033[1;35m' # Purple
colors[BCyan]='\033[1;36m' # Cyan
colors[BWhite]='\033[1;37m' # White
# Underline
colors[UBlack]='\033[4;30m' # Black
colors[URed]='\033[4;31m' # Red
colors[UGreen]='\033[4;32m' # Green
colors[UYellow]='\033[4;33m' # Yellow
colors[UBlue]='\033[4;34m' # Blue
colors[UPurple]='\033[4;35m' # Purple
colors[UCyan]='\033[4;36m' # Cyan
colors[UWhite]='\033[4;37m' # White
# Background
colors[On_Black]='\033[40m' # Black
colors[On_Red]='\033[41m' # Red
colors[On_Green]='\033[42m' # Green
colors[On_Yellow]='\033[43m' # Yellow
colors[On_Blue]='\033[44m' # Blue
colors[On_Purple]='\033[45m' # Purple
colors[On_Cyan]='\033[46m' # Cyan
colors[On_White]='\033[47m' # White
# High Intensity
colors[IBlack]='\033[0;90m' # Black
colors[IRed]='\033[0;91m' # Red
colors[IGreen]='\033[0;92m' # Green
colors[IYellow]='\033[0;93m' # Yellow
colors[IBlue]='\033[0;94m' # Blue
colors[IPurple]='\033[0;95m' # Purple
colors[ICyan]='\033[0;96m' # Cyan
colors[IWhite]='\033[0;97m' # White
# Bold High Intensity
colors[BIBlack]='\033[1;90m' # Black
colors[BIRed]='\033[1;91m' # Red
colors[BIGreen]='\033[1;92m' # Green
colors[BIYellow]='\033[1;93m' # Yellow
colors[BIBlue]='\033[1;94m' # Blue
colors[BIPurple]='\033[1;95m' # Purple
colors[BICyan]='\033[1;96m' # Cyan
colors[BIWhite]='\033[1;97m' # White
# High Intensity backgrounds
colors[On_IBlack]='\033[0;100m' # Black
colors[On_IRed]='\033[0;101m' # Red
colors[On_IGreen]='\033[0;102m' # Green
colors[On_IYellow]='\033[0;103m' # Yellow
colors[On_IBlue]='\033[0;104m' # Blue
colors[On_IPurple]='\033[0;105m' # Purple
colors[On_ICyan]='\033[0;106m' # Cyan
colors[On_IWhite]='\033[0;107m' # White
color=${colors[$input_color]}
white=${colors[White]}
# echo $white
for i in "${!colors[@]}"
do
echo -e "$i = ${colors[$i]}I love you$white"
done
后果
希望这张图片能帮助你选择适合你的颜色:D
其他回答
red='\e[0;31m'
NC='\e[0m' # No Color
echo -e "${red}Hello Stackoverflow${NC}"
这个答案是正确的,只是对颜色的调用不应该在引号内。
echo -e ${red}"Hello Stackoverflow"${NC}
应该会成功的。
感谢@k-five的回答
declare -A colors
#curl www.bunlongheng.com/code/colors.png
# Reset
colors[Color_Off]='\033[0m' # Text Reset
# Regular Colors
colors[Black]='\033[0;30m' # Black
colors[Red]='\033[0;31m' # Red
colors[Green]='\033[0;32m' # Green
colors[Yellow]='\033[0;33m' # Yellow
colors[Blue]='\033[0;34m' # Blue
colors[Purple]='\033[0;35m' # Purple
colors[Cyan]='\033[0;36m' # Cyan
colors[White]='\033[0;37m' # White
# Bold
colors[BBlack]='\033[1;30m' # Black
colors[BRed]='\033[1;31m' # Red
colors[BGreen]='\033[1;32m' # Green
colors[BYellow]='\033[1;33m' # Yellow
colors[BBlue]='\033[1;34m' # Blue
colors[BPurple]='\033[1;35m' # Purple
colors[BCyan]='\033[1;36m' # Cyan
colors[BWhite]='\033[1;37m' # White
# Underline
colors[UBlack]='\033[4;30m' # Black
colors[URed]='\033[4;31m' # Red
colors[UGreen]='\033[4;32m' # Green
colors[UYellow]='\033[4;33m' # Yellow
colors[UBlue]='\033[4;34m' # Blue
colors[UPurple]='\033[4;35m' # Purple
colors[UCyan]='\033[4;36m' # Cyan
colors[UWhite]='\033[4;37m' # White
# Background
colors[On_Black]='\033[40m' # Black
colors[On_Red]='\033[41m' # Red
colors[On_Green]='\033[42m' # Green
colors[On_Yellow]='\033[43m' # Yellow
colors[On_Blue]='\033[44m' # Blue
colors[On_Purple]='\033[45m' # Purple
colors[On_Cyan]='\033[46m' # Cyan
colors[On_White]='\033[47m' # White
# High Intensity
colors[IBlack]='\033[0;90m' # Black
colors[IRed]='\033[0;91m' # Red
colors[IGreen]='\033[0;92m' # Green
colors[IYellow]='\033[0;93m' # Yellow
colors[IBlue]='\033[0;94m' # Blue
colors[IPurple]='\033[0;95m' # Purple
colors[ICyan]='\033[0;96m' # Cyan
colors[IWhite]='\033[0;97m' # White
# Bold High Intensity
colors[BIBlack]='\033[1;90m' # Black
colors[BIRed]='\033[1;91m' # Red
colors[BIGreen]='\033[1;92m' # Green
colors[BIYellow]='\033[1;93m' # Yellow
colors[BIBlue]='\033[1;94m' # Blue
colors[BIPurple]='\033[1;95m' # Purple
colors[BICyan]='\033[1;96m' # Cyan
colors[BIWhite]='\033[1;97m' # White
# High Intensity backgrounds
colors[On_IBlack]='\033[0;100m' # Black
colors[On_IRed]='\033[0;101m' # Red
colors[On_IGreen]='\033[0;102m' # Green
colors[On_IYellow]='\033[0;103m' # Yellow
colors[On_IBlue]='\033[0;104m' # Blue
colors[On_IPurple]='\033[0;105m' # Purple
colors[On_ICyan]='\033[0;106m' # Cyan
colors[On_IWhite]='\033[0;107m' # White
color=${colors[$input_color]}
white=${colors[White]}
# echo $white
for i in "${!colors[@]}"
do
echo -e "$i = ${colors[$i]}I love you$white"
done
后果
希望这张图片能帮助你选择适合你的颜色:D
您可以“组合”颜色和文本模式。
#!/bin/bash
echo red text / black background \(Reverse\)
echo "\033[31;7mHello world\e[0m";
echo -e "\033[31;7mHello world\e[0m";
echo
echo yellow text / red background
echo "\033[32;41mHello world\e[0m";
echo -e "\033[32;41mHello world\e[0m";
echo "\033[0;32;41mHello world\e[0m";
echo -e "\033[0;32;41mHello world\e[0m";
echo
echo yellow BOLD text / red background
echo "\033[1;32;41mHello world\e[0m";
echo -e "\033[1;32;41mHello world\e[0m";
echo
echo yellow BOLD text underline / red background
echo "\033[1;4;32;41mHello world\e[0m";
echo -e "\033[1;4;32;41mHello world\e[0m";
echo "\033[1;32;4;41mHello world\e[0m";
echo -e "\033[1;32;4;41mHello world\e[0m";
echo "\033[4;32;41;1mHello world\e[0m";
echo -e "\033[4;32;41;1mHello world\e[0m";
echo
为了可读性
如果要提高代码的可读性,可以先回显字符串,然后使用sed添加颜色:
echo 'Hello World!' | sed $'s/World/\e[1m&\e[0m/'
使用tput计算颜色代码。避免使用ANSI转义码(例如,\e[31;1m表示红色),因为它不易移植。例如,OS X上的Bash不支持它。
BLACK=`tput setaf 0`
RED=`tput setaf 1`
GREEN=`tput setaf 2`
YELLOW=`tput setaf 3`
BLUE=`tput setaf 4`
MAGENTA=`tput setaf 5`
CYAN=`tput setaf 6`
WHITE=`tput setaf 7`
BOLD=`tput bold`
RESET=`tput sgr0`
echo -e "hello ${RED}some red text${RESET} world"