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

1、先上效果图

2、bash代码

#!/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
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!"

3、其他脚本中使用示例

example.sh

#!/bin/bash

#导入color.sh脚本,即可调用里面的函数(若exmaple.sh与color.sh不在同一目录,下面的导入记得使用color.sh的绝对路径)
. color.sh echo_red "red message"
echo_blue -b white "blue message, white background"

Bash echo输出带颜色和背景的文本的更多相关文章

  1. 让shell脚本中的echo输出带颜色

    转载自:http://www.linuxidc.com/Linux/2014-12/110463.htm 让echo输出带颜色的方法有两种 1.在shell脚本中定义颜色变量,使用echo -e调用变 ...

  2. echo如何输出带颜色的文本

    本文链接:https://blog.csdn.net/qualcent/article/details/7106483 ######################################## ...

  3. Bootstrap-全局样式的文本颜色和背景颜色

    .text-五种颜色   文本颜色.text-info文本浅蓝颜色-提示.text-warning文本黄色-警告颜色.text-success文本绿色-成功颜色.text-primary文本深蓝色-警 ...

  4. 【MFC系列】MFC快速设置控件文本字体、大小、颜色、背景

    以静态文本为例,分享一下怎么修改文本字体.大小.颜色.背景等参数.其他文本.控件等可参照修改. 1.修改字体.大小 这个很简单,首先在Dlg类中声明一个CFont类型的成员变量: 然后在类的初始化函数 ...

  5. echo -e "\033[字背景颜色;字体颜色m字符串\033[0m

    格式: echo -e "\033[字背景颜色;字体颜色m字符串\033[0m" 例如: echo -e "\033[41;36m something here \033 ...

  6. (转)TextView 设置背景和文本颜色的问题

    在做一个项目,突然遇到如下问题 比如:在color.xml中定义了几个颜色 <color name="white">#FFFFFF</color> < ...

  7. (转)shell脚本输出带颜色字体

    shell脚本输出带颜色字体 原文:http://blog.csdn.net/andylauren/article/details/60873400 输出特效格式控制:\033[0m  关闭所有属性  ...

  8. Shell~echo -e 颜色输出

    echo -e 可以控制字体颜色和背景颜色输出 从一个例子开始: # echo -e "\e[1;33;41m test content \e[0m" 输出效果: . \e 转义起 ...

  9. php 扫描url死链接 \033[31m ANSI Linux终端输出带颜色

    * 从Packagist上搜索需要的包 https://packagist.org/ * 通过composer下载依赖包 composer require guzzlehttp/guzzlecompo ...

随机推荐

  1. python3 进行接口测试

    最近有研究接口测试,然后查了查资料,发现有两种方法,一种是使用urllib库,一种是使用requests库.而在这里,我使用的是requests库,为什么要用这个呢? 从官方文档看出,python的标 ...

  2. Dell XPS 7590 Hackintosh

    网上主流引导Hackintosh的工具有Chameleon, Clover和OpenCore. 但是随着Hackintosh重要驱动开发团队acidanthera逐渐转向OpenCore,后者显然才是 ...

  3. PADS Layout VX.2.3 灌铜之后没有显示整块铜皮的原因

    操作系统:Windows 10 x64 工具1:PADS Layout VX.2.3 灌铜之后没有显示整块铜皮,如下图所示: 点击菜单Tools > Options...(快捷键:Ctrl + ...

  4. 【优化】单调队列与dp

    笔者大概看了一下单调队列对于DP的优化,故撰此文,望有帮助. (dp还是推式子难啊qwq) 例题1. 题目大意:在n个数的序列中,选择数字,使得其连续不超过k个数,且和最大. 本题的方程相对好推:设d ...

  5. 2018年10月份编程语言排行榜(来自TIOBE Index for October 2018)

    TIOBE Index for October 2018 from:https://www.tiobe.com/tiobe-index// October Headline: Swift is kno ...

  6. shell-批量修改文件名及扩展名多案例

    1. 功能描述如下表: 批量文件改名案例实战: 问题1:  创建测试数据 [root@1-241 tmp]# for i in `seq 6`;do touch stu_161226_${i}_fin ...

  7. LR Optimization-Based Estimator Design for Vision-Aided Inertial Navigation

    Abstract 我们设计了一个 hybrid 估计器, 组合了两种算法, sliding-window EKF 和 EKF-SLAM. 我们的结果表示, hybrid算法比单一的好. 1. Intr ...

  8. MeteoInfoLab脚本示例:Maskout图形

    Maskout通常有两种类型:Maskout图形和Maskout数据.这里是Maskout图形的示例.需要用shaperead读取地图数据形成图层作为Maskout图层(这里是中国的行政区域china ...

  9. 扫描仪扫描文件处理-ABBYY对扫描版PDF文字加黑加粗、去除背景漂白

    1. 设置ABBYY自动歪斜矫正: 2. 设置导出PDF参数: 3. PDF文字加黑加粗.去除背景漂白步骤:3.1 ABBYY - 打开扫描版PDF文档3.2 ABBYY - 编辑图像3.3 等级 - ...

  10. 【树】HNOI2014 米特运输

    题目大意 洛谷链接 给出一课点带权的树,修改一些点的权值使该树满足: 同一个父亲的儿子权值必须相同 父亲的取值必须是所有儿子权值之和 输入格式 第一行是一个正整数\(N\),表示节点的数目. 接下来\ ...