shell echo 显示不同颜色文本 背景色

#!/bin/bash    

# 颜色定义
# 黑色:0
BLACK=$(tput setaf 0)
BLACK_BG=$(tput setab 0)
# 红色:1
RED=$(tput setaf 1)
RED_BG=$(tput setab 1)
# 绿色:2
GREEN=$(tput setaf 2)
GREEN_BG=$(tput setab 2)
# 黄色:3
YELLOW=$(tput setaf 3)
YELLOW_BG=$(tput setab 3)
# 蓝色:4
BLUE=$(tput setaf 4)
BLUE_BG=$(tput setab 4)
# 洋红色:5
CARMINE=$(tput setaf 5)
CARMINE_BG=$(tput setab 5)
# 青色:6
CYAN=$(tput setaf 6)
CYAN_BG=$(tput setab 6)
# 白色:7
WHITE=$(tput setaf 7)
WHITE_BG=$(tput setab 7)
RESET=$(tput sgr0)
echo "${BLACK}黑色字体 This is a text.${RESET}"
echo "${RED}红色字体 This is a text.${RESET}"
echo "${GREEN}绿色字体 This is a text.${RESET}"
echo "${YELLOW}黄色字体 This is a text.${RESET}"
echo "${BLUE}蓝色字体$ This is a text.{RESET}"
echo "${CARMINE}洋红色字体 This is a text.${RESET}"
echo "${CYAN}青色 This is a text.${RESET}"
echo "${WHITE}白色字体 This is a text.${RESET}"
echo "${BLACK}黑色字体${RED}红色字体${GREEN}绿色字体${YELLOW}黄色字体${BLUE}蓝色字体${CARMINE}洋红色字体${CYAN}青色${WHITE}白色字体${RESET}"
echo ""
echo "${BLACK_BG}黑色背景 This is a text.${RESET}"
echo "${RED_BG}红色背景 This is a text.${RESET}"
echo "${GREEN_BG}绿色背景 This is a text.${RESET}"
echo "${YELLOW_BG}黄色背景 This is a text.${RESET}"
echo "${BLUE_BG}蓝色背景 This is a text.${RESET}"
echo "${CARMINE_BG}洋红色背景 This is a text.${RESET}"
echo "${CYAN_BG}青色 This is a text.${RESET}"
echo "${WHITE_BG}白色背景 This is a text.${RESET}"
echo "${BLACK_BG}黑色背景${RED_BG}红色背景${GREEN_BG}绿色背景${YELLOW_BG}黄色背景${BLUE_BG}蓝色背景${CARMINE_BG}洋红色背景${CYAN_BG}青色${WHITE_BG}白色背景${RESET}"
terminal黑色背景效果:

terminal白色背景效果:

# return 1

function test2(){
    echo "start test2()  ......"
        echo "2即将执行创建镜像操作,确认请按任意键继续,按'n'跳过创建镜像操作】:"
    read  confirm_opt
    confirm_opt=$(echo "$confirm_opt" | tr '[:upper:]' '[:lower:]')
    echo "confirm_opt=$confirm_opt"
     if [ "$confirm_opt" = "no" ] || [ "$confirm_opt" = "n" ]; then
        echo "test2()跳过创建镜像操作"
        return  1
     fi
    echo "end test2()  ......"
}

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Shell中使用echo打印彩色字体和彩色背景,可以使用转义序列来实现。转义序列是一种特殊的控制指令,用于告诉Shell执行特定的步骤。 具体方法如下所示: 1. 首先,定义echo命令的输出样式为变量。可以使用不同颜色代码来定义不同的样式。比如使用"\033[41;33;1m"来定义一个具有红色背景、黄色前景和加粗效果的样式。 2. 然后,使用echo命令来输出想要展示的内容。在输出的字符串前后使用转义序列,即"\033[字背景颜色;字体颜色m"和"\033[0m"。其中,字背景颜色和字体颜色可以选择不同颜色代码。 下面是一个示例: ``` # 定义echo命令的输出样式 export ECHO_STYLE_01="\033<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Shell命令:echo介绍,echo如何输出带颜色文本](https://blog.csdn.net/qualcent/article/details/7106483)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [shell中使用echo打印彩色字体和彩色背景的方法](https://download.csdn.net/download/weixin_38637983/12846501)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [shell脚本里使用echo输出颜色](https://blog.csdn.net/qq_39565646/article/details/89881554)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值