Linux 控制 Bash 输出的格式与颜色

📅 2026/7/11 10:31:08
Linux 控制 Bash 输出的格式与颜色
1. Bash 中的转义序列和转义字符转义序列 (escape sequences)由转义字符Escape character通常为 “^[” or “Esc”和其它字符组成“Esc[FormatCodem”。在 Bash 中“Esc” 可由下列符号替换\e\033\x1B例如echo -e \e[31mHello World\e[0mecho -e \033[31mHello\e[0m World-e选项使得echo命令可以解析转义序列。“\e[0m” 序列表示移除所有样式格式和颜色。最好在希望添加样式的内容后面加上消除对后面字符的影响。ANSI/VT100转义序列不仅可以在 Bash 中使用其它任何编程序言都可以使用。本文主要记录其在 Bash 中的作用。综上在 Bash 中使用转义序列控制格式和颜色可以使用如下规则”\e[控制字符m......\e[0m“2. 控制格式格式控制代码的兼容性与终端类型有关有些格式代码在有些终端不起作用.CodeFormatExample0取消前面的效果包括格式与颜色放于结尾1粗体高亮echo -e Hellow \e[1mHellow\e[0m2瘦体echo -e Hellow \e[2mHellow\e[0m3斜体echo -e Hellow \e[3mHellow\e[0m4下划线echo -e Hellow \e[4mHellow\e[0m5闪烁echo -e Hellow \e[5mHellow\e[0m7反色文字和背景颜色互换echo -e Hellow \e[7mHellow\e[0m8隐藏echo -e Hellow \e[8mHellow\e[0m#!/bin/bash for f in 1 2 3 4 5 7 ;do echo -e \e[${f}mHello World\e[0m done exit 03. 8/16 颜色颜色控制代码的兼容性与终端类型有关有些颜色代码在有些终端下不起作用。颜色的实际效果与终端类型有关。前景颜色文字颜色CodeColorExample39Default foreground colorecho -e Default \e[39mDefault\e[0m30Blackecho -e Default \e[30mBlack\e[0m31Redecho -e Default \e[31mRed\e[0m32Greenecho -e Default \e[32mGreen\e[0m33Yellowecho -e Default \e[33mYellow\e[0m|34Blueecho -e Default \e[34mBlue\e[0m35Magentaecho -e Default \e[35mMagenta\e[0m36Cyanecho -e Default \e[36mCyan\e[0m37Light grayecho -e Default \e[37mLight gray\e[0m90Dark grayecho -e Default \e[90mDark gray\e[0m91Light redecho -e Default \e[91mLight red\e[0m92Light greenecho -e Default \e[92mLight green\e[0m93Light yellowecho -e Default \e[93mLight yellow\e[0m94Light blueecho -e Default \e[94mLight blue\e[0m95Light magentaecho -e Default \e[95mLight magenta\e[0m96Light cyanecho -e Default \e[96mLight cyan\e[0m97Whiteecho -e Default \e[97mWhite\e[0m背景颜色CodeColorExample49Default background colorecho -e Default \e[49mDefault\e[0m40Blackecho -e Default \e[40mBlack\e[0m41Redecho -e Default \e[41mRed\e[0m42Greenecho -e Default \e[42mGreen\e[0m43Yellowecho -e Default \e[43mYellow\e[0m44Blueecho -e Default \e[44mBlue\e[0m45Magentaecho -e Default \e[45mMagenta\e[0m46Cyanecho -e Default \e[46mCyan\e[0m47Light grayecho -e Default \e[47mLight gray\e[0m100Dark grayecho -e Default \e[100mDark gray\e[0m101Light redecho -e Default \e[101mLight red\e[0m102Light greenecho -e Default \e[102mLight green\e[0m103Light yellowecho -e Default \e[103mLight yellow\e[0m104Light blueecho -e Default \e[104mLight blue\e[0m105Light magentaecho -e Default \e[105mLight magenta\e[0m106Light cyanecho -e Default \e[106mLight cyan\e[0m107Whiteecho -e Default \e[107mWhite\e[0m4. 88/256 颜色支持 256 色彩的仅vte(GNOME Terminal, XFCE4 Terminal, Nautilus Terminal, Terminator, …)。支持 88 色彩的终端例如rxvt显示的颜色与支持 256 色彩的终端显示的前 88 种颜色不同。c256.sh#!/bin/bash echo for fgbg in 38 48 ; do # Foreground / Background echo for color in {0..255} ; do # Colors # Display the color printf \e[${fgbg};5;%sm %3s \e[0m $color $color # Display 6 colors per lines if [ $((($color 1) % 8)) 0 ] ; then echo # New line fi done echo # New line done exit 0运行上面代码可以产生所有 256 色彩包括前景和背景色bash c256.sh前景颜色文字颜色转义序列模式为\e[38;5;Cm ... \e[0mC为下列数字例echo -e \e[38;5;82mHello \e[38;5;198mWorld\e[0m背景颜色转义序列模式为\e[38;5;Cm ... \e[0mC为下列数字例echo -e \e[48;5;82mHello\e[0m \e[48;5;198mWorld\e[0m5. 格式与色彩组合控制”\e[控制字符m......\e[0m“将下面代码用;连接排列放在上面控制字符的部分。不区分先后可多种效果叠加。格式代码FF∈{1234578}前景 8/16 色彩代码FgC16FgC16∈{30…373990…97}背景 8/16 色彩代码BgC16BgC16∈{40…4749100…107}前景 88/256 色彩代码38;5;C256C256∈{01…255}背景 88/256 色彩代码48;5;C256C256∈{01…255}下划线蓝底红字echo -e \e[4;31;44mHello World\e[0m高亮下划线蓝底红字echo -e \e[1;4;31;44mHello World\e[0m斜体下划线文字红色背景土黄色echo -e \e[3;4;31;48;5;100mHello World\e[0m斜体下划线文字46号色背景95号色echo -e \e[3;4;38;5;46;48;5;95mHello World\e[0m