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]#