在shell脚本里显示带颜色的字(linux)
一、echo命令(带参数-e) cat color.sh #!/bin/bash echo -e "\033[30m black \033[0m" echo -e "\033[31m red \033[0m" echo -e "\033[32m green \033[0m" echo -e "\033[33m yellow \033[0m" echo -e "\033[34m blue \033[0m" echo -e "\033[35m purple \033[0m" echo -e "\033[36m cyan \033[0m" echo -e "\033[37m white \033[0m" 执行结果: bash color.sh black red green yellow blue purple cyan white 二、printf命令 cat color2.sh #!/bin/bash printf "\033[30m black \033[0m\n" printf "\033[31m red \033[0m\n" printf "\033[32m green \033[0m\n" printf "\033[33m yellow \033[0m\n" printf "\033[34m blue \033[0m\n" printf "\033[35m purple \033[0m\n" printf "\033[36m cyan \033[0m\n" printf "\033[37m white \033[0m\n" 执行结果: bash color2.sh black red green yellow blue purple cyan white 三、使用变量 cat color3.sh #!/bin/bash blackcolor=‘\E[1;30m‘ redcolor=‘\E[1;31m‘ greencolor=‘\E[1;32m‘ yellowcolor=‘\E[1;33m‘ bluecolor=‘\E[1;34m‘ purplecolor=‘\E[1;35m‘ cyancolor=‘\E[1;36m‘ whitecolor=‘\E[1;37m‘ echo -e "${blackcolor}black" echo -e "${redcolor}red" echo -e "${greencolor}green" echo -e "${yellowcolor}yellow" echo -e "${bluecolor}blue" echo -e "${purplecolor}purple" echo -e "${cyancolor}cyan" echo -e "${whitecolor}white" 执行结果: bash color3.sh black red green yellow blue purple cyan white 四、系统函数 cat color4.sh #!/bin/bash blackcolor="echo -en \\033[1;30m" redcolor="echo -en \\033[1;31m" greencolor="echo -en \\033[1;32m" yellowcolor="echo -en \\033[1;33m" bluecolor="echo -en \\033[1;34m" purplecolor="echo -en \\033[1;35m" cyancolor="echo -en \\033[1;36m" whitecolor="echo -en \\033[1;37m" echo black && ${blackcolor} echo red && ${redcolor} echo green && ${greencolor} echo yellow && ${yellowcolor} echo blue && ${bluecolor} echo purple && ${purplecolor} echo cyan && ${cyancolor} echo white && ${whitecolor} 执行结果: bash color4.sh black red green yellow blue purple cyan white
本文出自 “7928217” 博客,请务必保留此出处http://7938217.blog.51cto.com/7928217/1651807
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。