bash自定义echo函数,支持设置文字颜色和背景颜色

 
更多
#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 显示光标

打赏

本文固定链接: https://www.cxy163.net/archives/3134 | 绝缘体

该日志由 绝缘体.. 于 2022年05月17日 发表在 未分类 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: bash自定义echo函数,支持设置文字颜色和背景颜色 | 绝缘体
关键字: , , , ,

bash自定义echo函数,支持设置文字颜色和背景颜色:等您坐沙发呢!

发表评论


快捷键:Ctrl+Enter