shell的技巧笔记

设置终端字符颜色

在shell中输出终端字符可以设置显式方式,颜色,底色。

控制字符串格式为:

  33[显示方式;前景色;背景色m

  或者

  e[显示方式;前景色;背景色m

控制字符结束为:

  e[0m或者33[0m

显式方式有以下几种:

  0(默认值)、1(高亮)、22(非粗体)、4(下划线)、24(非下划线)、 5(闪烁)、25(非闪烁)、7(反显)、27(非反显)

前景色典型的有以下几种:

  30(黑色)、31(红色)、32(绿色)、 33(黄色)、34(蓝色)、35(洋红)、36(青色)、37(白色)

背景色典型的有以下几种:

  40(黑色)、41(红色)、42(绿色)、 43(黄色)、44(蓝色)、45(洋红)、46(青色)、47(白色)

示例:

BOLD='e[1;31m'         # Bold Red
REV='e[1;32m'       # Bold Green
OFF='e[0m'
#Help function
function HELP {
  echo -e "${REV}Basic usage:${OFF} ${BOLD}$SCRIPT -d helloworld ${OFF}"\n
  echo -e "${REV}The following switches are recognized. $OFF "
  echo -e "${REV}-p ${OFF}  --Sets the environment to use for installing python ${OFF}. Default is ${BOLD} /usr/bin ${OFF}"
  echo -e "${REV}-d ${OFF}  --Sets the directory whose virtualenv is to be setup. Default is ${BOLD} local folder (.) ${OFF}"
  echo -e "${REV}-v ${OFF}  --Sets the python version that you want to install. Default is ${BOLD} 2.7 ${OFF}"
  echo -e "${REV}-h${OFF}  --Displays this help message. No further functions are performed."\n
  echo -e "Example: ${BOLD}$SCRIPT -d helloworld -p /opt/py27env/bin -v 2.7 ${OFF}"\n
  exit 1
}

HELP

得到显示结果如下:

原文地址:https://www.cnblogs.com/lifewithlight/p/10008145.html