如何在 Linux 终端中更改文本颜色。





5.00/5 (9投票s)
快速概览和一个简单的 bash 脚本,让您的脚本输出更生动。
引言
终端的默认文本输出是单色的,没有提供一个简单的方法来提供上下文。例如,您可能希望错误以红色显示,成功以绿色显示,或者重要信息以粗体输出。
向终端输出添加颜色非常简单,涉及在文本之前输出正确的控制字符。
终端颜色
要输出彩色文本,您需要使用 printf
或 echo -e
(使用 -e
确保解释控制字符)来输出所需颜色的控制字符,然后输出您的文本,然后(为了整洁)将输出重置为默认值。下表列出了代码
Color | Foreground | 背景 |
---|---|---|
默认值 | \033[39m | \033[49m |
黑色 | \033[30m | \033[40m |
深红色 | \033[31m | \033[41m |
深绿色 | \033[32m | \033[42m |
深黄色(橙色) | \033[33m | \033[43m |
深蓝色 | \033[34m | \033[44m |
深品红色 | \033[35m | \033[45m |
深青色 | \033[36m | \033[46m |
浅灰色 | \033[37m | \033[47m |
深灰色 | \033[90m | \033[100m |
红色 | \033[91m | \033[101m |
绿色 | \033[92m | \033[101m |
橙色 | \033[93m | \033[103m |
蓝色 | \033[94m | \033[104m |
品红色 | \033[95m | \033[105m |
青色 | \033[96m | \033[106m |
白色 | \033[97m | \033[107m |
重置代码是 \033[0m
前景色字符串的格式是
"\033[" + "<0 or 1, meaning normal or bold>;" + "<color code> + "m"
背景色字符串的格式是
"\033[" + "<color code>" + "m"
这些代码可以一起输出以同时更改前景色和背景色。
Using the Code
红色文本的简单示例
printf "\033[91mThis is red text\033[0m"
白色背景上的红色文本示例
printf "\033[91m\033[107mThis is red text on a white background\033[0m"
这有点麻烦,所以我创建了一些简单的子例程,这些例程提供了以更文明的方式输出文本的方法。
辅助函数
以下辅助函数允许您执行类似的操作
WriteLine "This is red text" "Red"
WriteLine "This is red text on a white background" "Red" "White"
容易得多。
useColor="true" # Set to false if you find your environment just doesn't handle colors well
# Returns a color code for the given foreground/background colors
# This code is echoed to the terminal before outputting text in
# order to generate a colored output.
#
# string foreground color name. Optional if no background provided.
# Defaults to "Default" which uses the system default
# string background color name. Optional. Defaults to $color_background
# which is set based on the current terminal background
# returns a string
function Color () {
local foreground=$1
local background=$2
if [ "$foreground" == "" ]; then foreground="Default"; fi
if [ "$background" == "" ]; then background="$color_background"; fi
local colorString='\033['
# Foreground Colours
case "$foreground" in
"Default") colorString='\033[0;39m';;
"Black" ) colorString='\033[0;30m';;
"DarkRed" ) colorString='\033[0;31m';;
"DarkGreen" ) colorString='\033[0;32m';;
"DarkYellow" ) colorString='\033[0;33m';;
"DarkBlue" ) colorString='\033[0;34m';;
"DarkMagenta" ) colorString='\033[0;35m';;
"DarkCyan" ) colorString='\033[0;36m';;
"Gray" ) colorString='\033[0;37m';;
"DarkGray" ) colorString='\033[1;90m';;
"Red" ) colorString='\033[1;91m';;
"Green" ) colorString='\033[1;92m';;
"Yellow" ) colorString='\033[1;93m';;
"Blue" ) colorString='\033[1;94m';;
"Magenta" ) colorString='\033[1;95m';;
"Cyan" ) colorString='\033[1;96m';;
"White" ) colorString='\033[1;97m';;
*) colorString='\033[0;39m';;
esac
# Background Colours
case "$background" in
"Default" ) colorString="${colorString}\033[49m";;
"Black" ) colorString="${colorString}\033[40m";;
"DarkRed" ) colorString="${colorString}\033[41m";;
"DarkGreen" ) colorString="${colorString}\033[42m";;
"DarkYellow" ) colorString="${colorString}\033[43m";;
"DarkBlue" ) colorString="${colorString}\033[44m";;
"DarkMagenta" ) colorString="${colorString}\033[45m";;
"DarkCyan" ) colorString="${colorString}\033[46m";;
"Gray" ) colorString="${colorString}\033[47m";;
"DarkGray" ) colorString="${colorString}\033[100m";;
"Red" ) colorString="${colorString}\033[101m";;
"Green" ) colorString="${colorString}\033[102m";;
"Yellow" ) colorString="${colorString}\033[103m";;
"Blue" ) colorString="${colorString}\033[104m";;
"Magenta" ) colorString="${colorString}\033[105m";;
"Cyan" ) colorString="${colorString}\033[106m";;
"White" ) colorString="${colorString}\033[107m";;
*) colorString="${colorString}\033[49m";;
esac
echo "${colorString}"
}
# Outputs a line, including linefeed, to the terminal using the given foreground / background
# colors
#
# string The text to output. Optional if no foreground provided. Default is just a line feed.
# string Foreground color name. Optional if no background provided. Defaults to "Default" which
# uses the system default
# string Background color name. Optional. Defaults to $color_background which is set based on the
# current terminal background
function WriteLine () {
local resetColor='\033[0m'
local str=$1
local forecolor=$2
local backcolor=$3
if [ "$str" == "" ]; then
printf "\n"
return;
fi
# Note the use of the format placeholder %s. This allows us to pass "--" as strings without error
if [ "$useColor" == "true" ]; then
local colorString=$(Color ${forecolor} ${backcolor})
printf "${colorString}%s${resetColor}\n" "${str}"
else
printf "%s\n" "${str}"
fi
}
# Outputs a line without a linefeed to the terminal using the given foreground / background colors
#
# string The text to output. Optional if no foreground provided. Default is just a line feed.
# string Foreground color name. Optional if no background provided. Defaults to "Default" which
# uses the system default
# string Background color name. Optional. Defaults to $color_background which is set based on the
# current terminal background
function Write () {
local resetColor="\033[0m"
local forecolor=$1
local backcolor=$2
local str=$3
if [ "$str" == "" ]; then
return;
fi
# Note the use of the format placeholder %s. This allows us to pass "--" as strings without error
if [ "$useColor" == "true" ]; then
local colorString=$(Color ${forecolor} ${backcolor})
printf "${colorString}%s${resetColor}" "${str}"
else
printf "%s" "$str"
fi
}
处理深色模式和浅色模式
在许多 Linux 和 Unix 发行版中,终端是黑色背景,白色文本。在 macOS 上,默认是白色背景,黑色文本。有 方法 可以测试和猜测发生了什么,但最终您需要在您的系统上测试并确定什么对您有效。
我所做的是假设,如果我们在 mac 上,那么我们可以直接测试,否则我们假设黑底白字。然后,我也尝试坚持使用默认颜色,并且如果我想要一些颜色,我将坚持使用一些我知道可以在任何地方运行良好的预定义颜色。
# Gets the terminal background color. It's a very naive guess
# returns an RGB triplet, values from 0 - 64K
function getBackground () {
if [[ $OSTYPE == 'darwin'* ]]; then
osascript -e \
'tell application "Terminal"
get background color of selected tab of window 1
end tell'
else
# See https://github.com/rocky/shell-term-background/blob/master/term-background.bash
# for a comprehensive way to test for background colour. For now we're just going to
# assume that non-macOS terminals have a black background.
echo "0,0,0" # we're making assumptions here
fi
}
# Determines whether or not the current terminal is in dark mode (dark background, light text)
# returns "true" if running in dark mode; false otherwise
function isDarkMode () {
local bgColor=$(getBackground)
IFS=','; colors=($bgColor); IFS=' ';
# Is the background more or less dark?
if [ ${colors[0]} -lt 20000 ] && [ ${colors[1]} -lt 20000 ] && [ ${colors[2]} -lt 20000 ]; then
echo "true"
else
echo "false"
fi
}
一旦我们对我们正在处理的事情有了一个大致的了解,我就会执行
darkmode=$(isDarkMode)
# Setup some predefined colours. Note that we can't reliably determine the background
# color of the terminal so we avoid specifically setting black or white for the foreground
# or background. You can always just use "White" and "Black" if you specifically want
# this combo, but test thoroughly
if [ "$darkmode" == "true" ]; then
color_primary="Blue"
color_mute="Gray"
color_info="Yellow"
color_success="Green"
color_warn="DarkYellow"
color_error="Red"
else
color_primary="DarkBlue"
color_mute="Gray"
color_info="Magenta"
color_success="DarkGreen"
color_warn="DarkYellow"
color_error="Red"
fi
要使用这些,请执行
WriteLine "Predefined colors on default background"
WriteLine
WriteLine "Default colored text" "Default"
WriteLine "Primary colored text" $color_primary
WriteLine "Mute colored text" $color_mute
WriteLine "Info colored text" $color_info
WriteLine "Success colored text" $color_success
WriteLine "Warning colored text" $color_warn
WriteLine "Error colored text" $color_error
WriteLine
WriteLine "Default color on predefined background"
WriteLine
WriteLine "Default colored background" "Default"
WriteLine "Primary colored background" "Default" $color_primary
WriteLine "Mute colored background" "Default" $color_mute
WriteLine "Info colored background" "Default" $color_info
WriteLine "Success colored background" "Default" $color_success
WriteLine "Warning colored background" "Default" $color_warn
WriteLine "Error colored background" "Default" $color_error
在经典的 Linux 终端上
在 macOS 终端上
事情有点模糊,所以让我们添加一个额外的函数,它将在我们选择的任何背景上提供对比的前景。
# Returns the name of a color that will providing a contrasting foreground
# color for the given background color. This function assumes $darkmode has
# been set globally.
#
# string background color name.
# returns a string representing a contrasting foreground colour name
function ContrastForeground () {
local color=$1
if [ "$color" == "" ]; then color="Default"; fi
if [ "$darkmode" == "true" ]; then
case "$color" in
"Default" ) echo "White";;
"Black" ) echo "White";;
"DarkRed" ) echo "White";;
"DarkGreen" ) echo "White";;
"DarkYellow" ) echo "White";;
"DarkBlue" ) echo "White";;
"DarkMagenta" ) echo "White";;
"DarkCyan" ) echo "White";;
"Gray" ) echo "Black";;
"DarkGray" ) echo "White";;
"Red" ) echo "White";;
"Green" ) echo "White";;
"Yellow" ) echo "Black";;
"Blue" ) echo "White";;
"Magenta" ) echo "White";;
"Cyan" ) echo "Black";;
"White" ) echo "Black";;
*) echo "White";;
esac
else
case "$color" in
"Default" ) echo "Black";;
"Black" ) echo "White";;
"DarkRed" ) echo "White";;
"DarkGreen" ) echo "White";;
"DarkYellow" ) echo "White";;
"DarkBlue" ) echo "White";;
"DarkMagenta" ) echo "White";;
"DarkCyan" ) echo "White";;
"Gray" ) echo "Black";;
"DarkGray" ) echo "White";;
"Red" ) echo "White";;
"Green" ) echo "Black";;
"Yellow" ) echo "Black";;
"Blue" ) echo "White";;
"Magenta" ) echo "White";;
"Cyan" ) echo "Black";;
"White" ) echo "Black";;
*) echo "White";;
esac
fi
echo "${colorString}"
}
然后,在 Color
子例程中,我们可以执行
function Color () {
local foreground=$1
local background=$2
if [ "$foreground" == "" ]; then foreground="Default"; fi
if [ "$background" == "" ]; then background="$color_background"; fi
if [ "$foreground" == "Contrast" ]; then
foreground=$(ContrastForeground ${background})
fi
...
为了使我们的带有背景色的文本更易读,我们执行
WriteLine
WriteLine "Default contrasting color on predefined background"
WriteLine
WriteLine "Primary colored background" "Contrast" $color_primary
WriteLine "Mute colored background" "Contrast" $color_mute
WriteLine "Info colored background" "Contrast" $color_info
WriteLine "Success colored background" "Contrast" $color_success
WriteLine "Warning colored background" "Contrast" $color_warn
WriteLine "Error colored background" "Contrast" $color_error
在 Linux 终端上的结果
...以及在 macOS 终端上