前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Bash echo输出带颜色和背景的文本

Bash echo输出带颜色和背景的文本

作者头像
程序员小涛
发布2020-12-03 11:36:27
2.1K0
发布2020-12-03 11:36:27
举报
文章被收录于专栏:涛的程序人生

Bash echo输出带颜色和背景的文本

1、先上效果图

colorful
colorful

2、bash代码

代码语言:javascript
复制
#!/bin/bash
#*************************************************************
#Author:    yangruitao
#Date:      2020-11-04
#FileName:  color.sh
#*************************************************************
black="0"
red="1"
green="2"
yellow="3"
blue="4"
magenta="5"
cyan="6"
white="7"

# Color 为文本和背景设置颜色
function Color() {
    Content=""
    Fg="3$1"
    Bg="4$2"
    SetColor="\e[$Fg;${Bg}m "
    EndColor=" \e[0m"
    for((i=3;i<=$#;i++)); do
        j=${!i}
        Content="${Content} $j "
    done
    echo -e ${SetColor}${Content}${EndColor}
}

# echo_black 输出黑色文本 可加背景颜色参数(背景默认不设置)
function echo_black() {
    if [ "$1" == "-b" ]; then
        Bg=$(($2))
        Content=$3
    else
        Bg="8"
        Content=$1
    fi
    Color $black $Bg $Content
}

# echo_red 输出红色文本 可加背景颜色参数(背景默认不设置)
function echo_red() {
   if [ "$1" == "-b" ]; then
        Bg=$(($2))
        Content=$3
    else
        Bg="8"
        Content=$1
    fi
    Color $red $Bg $Content
}

# echo_green 输出绿色文本 可加背景颜色参数(背景默认不设置)
function echo_green() {
    if [ "$1" == "-b" ]; then
        Bg=$(($2))
        Content=$3
    else
        Bg="8"
        Content=$1
    fi
    Color $green $Bg $Content
}

# echo_yellow 输出黄色文本 可加背景颜色参数(背景默认不设置)
function echo_yellow() {
    if [ "$1" == "-b" ]; then
        Bg=$(($2))
        Content=$3
    else
        Bg="8"
        Content=$1
    fi
    Color $yellow $Bg $Content
}

# echo_blue 输出蓝色文本 可加背景颜色参数(背景默认不设置)
function echo_blue() {
    if [ "$1" == "-b" ]; then
        Bg=$(($2))
        Content=$3
    else
        Bg="8"
        Content=$1
    fi
    Color $blue $Bg $Content
}

# echo_magenta 输出洋红色文本 可加背景颜色参数(背景默认不设置)
function echo_magenta() {
    if [ "$1" == "-b" ]; then
        Bg=$(($2))
        Content=$3
    else
        Bg="8"
        Content=$1
    fi
    Color $magenta $Bg $Content
}

# echo_cyan 输出青色文本 可加背景颜色参数(背景默认不设置)
function echo_cyan() {
    if [ "$1" == "-b" ]; then
        Bg=$(($2))
        Content=$3
    else
        Bg="8"
        Content=$1
    fi
    Color $cyan $Bg $Content
}

# echo_white 输出白色文本 可加背景颜色参数(背景默认不设置)
function echo_white() {
    if [ "$1" == "-b" ]; then
        Bg=$(($2))
        Content=$3
    else
        Bg="8"
        Content=$1
    fi
    Color $white $Bg $Content
}

#main 防止导入其他脚本中使用时,会输出以下内容
case $1 in
    show)
    echo -e "example ... [\e[1;30m black \e[0m|\e[1;31m red \e[0m|\e[1;32m green \e[0m|\e[1;33m yellow \e[0m|\e[1;34m blue \e[0m|\e[1;35m magenta \e[0m|\e[1;36m cyan \e[0m|\e[1;37m white \e[0m]"
    echo -en "using [\e[1;40m echo_black \"hello\"\e[0m ] to output black text: "
    echo_black "hello"
    echo -en "using [\e[1;41m echo_red \"hello\" \e[0m] to output red text: "
    echo_red "hello"
    echo -en "using [\e[1;42m echo_green \"hello\" \e[0m] to output green text: "
    echo_green "hello"
    echo -en "using [\e[1;43m echo_yellow \"hello\" \e[0m] to output yellow text: "
    echo_yellow "hello"
    echo -en "using [\e[1;44m echo_blue \"hello\" \e[0m] to output blue text: "
    echo_blue "hello"
    echo -en "using [\e[1;45m echo_magenta \"hello\" \e[0m] to output magenta text: "
    echo_magenta "hello"
    echo -en "using [\e[1;46m echo_cyan \"hello\" \e[0m] to output cyan text: "
    echo_cyan "hello"
    echo -en "using [\e[1;47m echo_white \"hello\" \e[0m] to output white text: "
    echo_white "hello"
    echo -en "using [\e[30;47m echo_black -b white \"hello,world!\" \e[0m] to output black text with white background: "
    echo_black -b white "hello, world!"
    ;;
    *)
    ;;
esac

3、其他脚本中使用示例

example.sh

代码语言:javascript
复制
#!/bin/bash

#导入color.sh脚本,即可调用里面的函数(若exmaple.sh与color.sh不在同一目录,下面的导入记得使用color.sh的绝对路径)
. color.sh

echo_red "red message"
echo_blue -b white "blue message, white background"
example
example
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/11/04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Bash echo输出带颜色和背景的文本
    • 1、先上效果图
      • 2、bash代码
        • 3、其他脚本中使用示例
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档