shell补-特殊玩法-color颜色实战

shell补-特殊玩法-color颜色实战

格式

echo -e "\e[1;31m红色字mygirl\E[0m"

\E 也可以用\033替代。开始结尾符号(大小写不区分)

[1数字1表示加粗显示(这个位置可以加不同的数字代表不同的意思,;eg1是加粗,5是blink闪烁.详细信息man console_codes 获得)

31m表示为红色字体,这个可以换不同的数字,代表不同的颜色“红色字mygirl”

0m表示关闭所有属性,这个位置可以换不同的数字,以代表不同的意思

set red foreground #foreground 前台颜色 字体颜色

set green background #background 背景颜色

[root@localhost ~]# echo -e "\e[1;31m红色字体mygril\e[0m"
红色字体mygril
[root@localhost ~]# echo -e "\e[1;32m绿色字体mygril\e[0m"
绿色字体mygril

#################经典案例
[root@localhost color]# cat testcolor.sh 
#!/bin/bash

red="\e[1;31m"
green="\e[1;32m"
end="\e[0m"
login()
{
cat  <<  EOFS
	1.input [red|r]    and your content set red foreground!
	2.input [green|g]  and your content set green foreground! 
EOFS
}
input(){
read  colors  content
case  "${colors}" in
 "red"|"r")
	echo -e "${red}${content}${end}"
 ;;
 "green"|"g")
	echo -e "${green}${content}${end}"
;;
 *)
	echo "you must inpuut {red|r|green|g}"
;;
esac
}
main(){
login
input
}
main
[root@localhost color]# bash testcolor.sh 

	1.input [red|r]    and your content set red foreground!
	2.input [green|g]  and your content set green foreground! 
r hai
hai             ###该内容会显示红色
[root@localhost color]# bash testcolor.sh 

	1.input [red|r]    and your content set red foreground!
	2.input [green|g]  and your content set green foreground! 
g come
come              ####会显示绿色
[root@localhost color]# 

posted @   翻滚的小井蛙  阅读(33)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
点击右上角即可分享
微信分享提示