使用sh命令时在终端上显示彩色输出

时间:2020-08-28 14:43:30

标签: linux bash shell

color.sh

 RED='\u001b[31m'
 GREEN='\u001b[32m'
 BLUE='\u001b[34m'
 YELLOW='\u001b[33m'
 WHITE='\u001b[37m'

 # use echo -e to print in certain colors
 echo -e ${RED} RED
 echo -e ${YELLOW} YELLOW
 echo -e ${GREEN} GREEN
 echo -e ${BLUE} BLUE
 echo -e ${WHITE} WHITE

在键入$ ./color.sh时,终端将以相应的颜色输出。但是,当我键入$ sh color.sh时,它不会以正确的颜色输出。相反,它给了我下面的输出。

-e \u001b[31m RED
-e \u001b[33m YELLOW
-e \u001b[32m GREEN
-e \u001b[34m BLUE
-e \u001b[37m WHITE

有什么方法可以使$ sh color.sh以相应的颜色打印?谢谢。

1 个答案:

答案 0 :(得分:3)

使用此代码

var activityIndicator: UIActivityIndicatorView

func presentLoadingView() {
    if containerView == nil {
        containerView = UIView(frame: self.view.bounds)
        view.addSubview(containerView)
        containerView.backgroundColor = .systemBackground
        containerView.alpha = 0
        UIView.animate(withDuration: 0.3) { self.containerView.alpha = 0.8 }
        activityIndicator = UIActivityIndicatorView(style: .large)
        containerView.addSubview(activityIndicator)
        activityIndicator.centerX(in: containerView, constant: 0)
        activityIndicator.centerY(in: containerView, constant: 0)
    }
    else { 
        self.containerView.isHidden = false
        activityIndicator.startAnimating()
    }
}

func dismissLoadingView() {
    DispatchQueue.main.async {
        self.activityIndicator.stopAnimating()
        self.containerView.isHidden = true
   }
}

但是最好使用 RED='\e[31m' GREEN='\e[32m' BLUE='\e[34m' YELLOW='\e[33m' WHITE='\e[37m'

printf

也看看here