简体   繁体   English

执行 shell 脚本时使用回显选项

[英]Use echo options when execution shell script

On mac OSX, I have this script:在 mac OSX 上,我有这个脚本:

#!/usr/local/bin/bash
echo -e "\e[41mError: Some error.\e[0m"

When I just run the echo -e... in a console, it prints the colored text "Error: Some error."当我在控制台中运行echo -e...时,它会打印彩色文本“错误:某些错误”。

When executed as a script sh myscript.sh , it litterally prints the flag and the escape characters: -e "\e[41mError: Some error.\e[0m" .当作为脚本sh myscript.sh执行时,它会打印标志和转义字符: -e "\e[41mError: Some error.\e[0m"

When I add the script location to ~/.bash_profile and execute it as myscript.sh , it does work.当我将脚本位置添加到~/.bash_profile并将其作为myscript.sh执行时,它确实有效。 But I need to be able execute it without adding it to my bash profile.但我需要能够在不将其添加到我的 bash 配置文件的情况下执行它。

Edit: using printf works: printf "\e[41mError: Some error.\e[0m\n" .编辑:使用 printf 工作: printf "\e[41mError: Some error.\e[0m\n"

when you run the shell with sh it runs in posix compatibility mode (ie as the bourne shell does)当您使用sh运行 shell 时,它以 posix 兼容模式运行(即,正如 bourne shell 所做的那样)

bash is a successor to this shell, one of the features it adds is the -e switch to echo bash 是此 shell 的继任者,它添加的功能之一是 -e 切换回声

in posix shell you don't need the -e, the escapes will be evaluated anyway在posix shell中你不需要-e,无论如何都会评估转义

in bash you do, so if you want to run bash do so explicitly在 bash 你这样做,所以如果你想运行 bash 明确这样做

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM