Shell 终端颜色控制
Shell通过转义字符序列来控制颜色和光标等行为
格式:
echo -e "\033[Attribute code;Frontground color code;Background color code m"
Attribute code, Frontground, Background 是可选的,可以只有其中一个选项
echo -e的目的是让echo识别转移字符
\033是表示escape的八进制表示
Escape sequence | Attribute |
— | — |
0 | 关闭所有属性 none |
1 | 加粗 bold |
4 | 下划线 underscore |
5 | 闪烁 blink |
7 | 反显 reverse |
8 | 消影 concealed |
nA | 光标上移n行 |
nB | 光标下移n行 |
nC | 光标右移n列 |
nD | 光标左移n列 |
y;xH | 设置光标位置 |
2J | 清屏 |
K | 清除从光标到行尾的内容 |
s | 保存光标位置 |
u | 恢复光标位置 |
?25l | 隐藏光标 |
?25h | 显示光标 |
Color | Front ground | Back ground |
— | — | — |
黑色 black | 30 | 40 |
红色 red | 31 | 41 |
绿色 green | 32 | 42 |
黄色 yellow | 33 | 43 |
蓝色 blue | 34 | 44 |
紫色 purple | 35 | 45 |
青色 cyan | 36 | 46 |
白色 white | 37 | 47 |
例:
echo -e "\033[0;30m Color test - Front Black!\033[0m" echo -e "\033[0;31m Color test - Front Red!\033[0m" echo -e "\033[0;32m Color test - Front Green!\033[0m" echo -e "\033[0;33m Color test - Front Yellow!\033[0m" echo -e "\033[0;34m Color test - Front Blue!\033[0m" echo -e "\033[0;35m Color test - Front Purple!\033[0m" echo -e "\033[0;36m Color test - Front Cyan!\033[0m" echo -e "\033[0;37m Color test - Front White!\033[0m" echo echo -e "\033[0;30;47m Color test - Front Black, Back White!\033[0m" echo -e "\033[0;31;46m Color test - Front Red, Back Cyan!\033[0m" echo -e "\033[0;32;45m Color test - Front Green, Back Purple!\033[0m" echo -e "\033[0;33;44m Color test - Front Yellow,Back Blue!\033[0m" echo -e "\033[0;34;43m Color test - Front Blue, Back Yellow!\033[0m" echo -e "\033[0;35;42m Color test - Front Purple,Back Green!\033[0m" echo -e "\033[0;36;41m Color test - Front Cyan, Back Red!\033[0m" echo -e "\033[0;37;40m Color test - Front White, Back Black!\033[0m"
效果如下: ![Escape Sequence Color test][Color_Test]