May
17
2022
bash自定义echo函数,支持设置文字颜色和背景颜色
作者:
绝缘体.. 发布:
2022-05-17 15:59 分类:
未分类 阅读:
抢沙发
#bash自定义echo函数,支持设置文字颜色和背景颜色
function customEcho() {
#$1 内容
#$2 文字颜色
#$3 背景颜色
fcolor="37"
bcolor="40"
if [ "$2" == "black" ]; then
fcolor="30"
elif [ "$2" == "red" ]; then
fcolor="31"
elif [ "$2" == "green" ]; then
fcolor="32"
elif [ "$2" == "yellow" ]; then
fcolor="33"
elif [ "$2" == "blue" ]; then
fcolor="34"
elif [ "$2" == "white" ]; then
fcolor="37"
fi
if [ "$3" == "black" ]; then
bcolor="40"
elif [ "$3" == "red" ]; then
bcolor="41"
elif [ "$3" == "green" ]; then
bcolor="42"
elif [ "$3" == "yellow" ]; then
bcolor="43"
elif [ "$3" == "blue" ]; then
bcolor="44"
elif [ "$3" == "white" ]; then
bcolor="47"
fi
#格式 echo -e "\033[字背景颜色;字体颜色m字符串\033[控制码"
echo -e "\e[${fcolor};${bcolor}m $1 \e[0m"
}
示例:
customEcho "www.phpernote.com" red
下面是详细说明:
字体背景颜色
40:黑
41:深红
42:绿
43:黄色
44:蓝色
45:紫色
46:深绿
47:白色
字体颜色
30:黑
31:红
32:绿
33:黄
34:蓝色
35:紫色
36:深绿
37:白色
控制码
这里常用有 设置高亮度/下划线/闪烁/关闭所有属性
\33[0m 关闭所有属性
\33[01m 设置高亮度
\33[04m 下划线
\33[05m 闪烁
\33[07m 反显
\33[08m 消隐
\33[30m — \33[37m 设置前景色
\33[40m — \33[47m 设置背景色
\33[nA 光标上移n行
\33[nB 光标下移n行
\33[nC 光标右移n行
\33[nD 光标左移n行
\33[y;xH设置光标位置
\33[2J 清屏
\33[K 清除从光标到行尾的内容
\33[s 保存光标位置
\33[u 恢复光标位置
\33[?25l 隐藏光标
\33[?25h 显示光标
微信扫一扫,打赏作者吧~