在bash中回显打印-e [英] echo printing -e in bash

查看:112
本文介绍了在bash中回显打印-e的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经获得了我在Bash中创建的脚本,我正在使用的功能之一是echo,并且我正在使用-e标志来解释\反斜杠转义.我有一个脚本以彩色打印文本,但是当它回显将要彩色的消息时,它还会打印带有消息的-e标志

I've gotten this script I've created in Bash, and one of the functions I'm using is echo and I'm using the -e flag for interpretations of \ backslash escapes. I have a script that prints text in color, but when it's echoing out the message that's gonna be in color it also prints the -e flag with the message

这里的例子;

NC='\033[31;0m\'       # no colors or formatting
RED='\033[0;31;1m\'    # print text in bold red
PUR='\033[0;35;1m\'    # print text in bold purple
YEL='\033[0;33;1m\'    # print text in bold Yellow
GRA='\033[0;37;1m\'    # print text in bold Gray

echo -e "This ${YEL}Message${NC} has color\nwith ${RED}new${NC} lines.

输出:

-e This Message has color.
with new lines

如果我也碰巧在bash脚本中要运行另一个命令,即使它不应该执行,我也会得到它.例如,在脚本中开始使用screen进行运行.

and if I also happen to have another command to be run in the bash script I also get this even though it shouldn't. For example with running with screen at the start of running this in a script.

\033[31;0m\033[0;31;1mscreen: not found


编辑**


EDIT**

要详细说明我要做什么;

To detail more with what I'm trying to do;

#!/bin/sh

##
## Nexus Miner script
##


NC='\033[31;0m'
RED='\033[0;31;1m'
PUR='\033[0;35;1m'
YEL='\033[0;33;1m'
GRA='\033[0;37;1m'

ADDR="<wallet-address-goes-here"
NAME="<worker-name-goes-here>"
URL="nexusminingpool.com"

## check if user is root, if true, then exit with status 1, else run program.
if [ `whoami` = root ]; then
   echo -e "\n${PUR}You don't need ROOT for this!${NC}\n" 1>&2
       exit 1;

## running Nexus Miner with Screen so you can run it in background
## and call it up at any time to check mining progress of Nexus CPU Miner.
   else
       echo -e "\n\n${YEL}Normal User?${NC} ${GRA}[OK]${NC}\n${GRA}Session running with${NC} ${RED}SCREEN${NC}${GRA}, Press${NC} ${RED}^C + A${NC} ${GRA}then${NC} ${RED}^C + D${NC} ${GRA}to run in background.${NC}\n${GRA}to resume enter `${NC}${RED}screen -r${NC}${GRA}` from Terminal.${NC}\n\n"
       echo -e "${PUR}Nexus CPU Miner${NC}\n${GRA}Wallet Address:${NC} $ADDR\n${GRA}Worker Name:${NC} $NAME\n${GRA}CPU Threads:${NC} (Default: 2)\n\n"

       ## call strings for wallet address and worker name varibles followe by thread numbers (default: 2)
       ## run nexus cpu miner within screen so it can run in the background
       `screen /home/user/PrimePoolMiner/nexus_cpuminer $URL 9549 $ADDR $NAME 2`
fi

推荐答案

您写道:我已经获得了我在Bash中创建的脚本",但您没有告诉我们您的确切含义.

You wrote, "I've gotten this script I've created in Bash", but you haven't told us exactly what you mean by that.

更新:问题已更新.脚本的第一行是#!/bin/sh.继续阅读以获取解释和解决方案.

UPDATE : The question has been updated. The script's first line is #!/bin/sh. Read on for an explanation and solution.

我可以通过将您的代码包含在以开头的脚本中来重现系统上的问题

I can reproduce the problem on my system by including your code in a script starting with

#!/bin/sh

我可以通过将第一行更改为

I can correct the problem by changing that first line to

#!/bin/bash

我系统上的

/bin/sh恰好是与dash的符号链接. dash shell具有echo作为内置命令,但不支持-e选项. #!行,

/bin/sh on my system happens to be a symlink to dash. The dash shell has echo as a builtin command, but it doesn't support the -e option. The #! line, g othe

echo命令的实现方式很多:大多数shell将其作为内置命令提供(具有各种功能,具体取决于shell),并且可能会有一个外部命令/bin/echo,其行为略有不同.

There are numerous implementations of the echo command: most shells provide it as a builtin command (with various features depending on the shell), and there's likely to be an external command /bin/echo with its own subtly different behavior.

如果您想要一致的行为来打印除简单文本行以外的任何内容,建议您使用printf命令.参见 https://unix.stackexchange.com/questions/65803/why-is -printf胜于回声(乔希·李(Josh Lee)在评论中引用).

If you want consistent behavior for printing anything other than a simple line of text, I suggest using the printf command. See https://unix.stackexchange.com/questions/65803/why-is-printf-better-than-echo (cited by Josh Lee in a comment).

#!行,称为 shebang ,控制用于执行脚本的shell.从中执行脚本的交互式外壳程序是无关紧要的. (几乎必须是这种方式;否则脚本针对不同用户的行为会有所不同).在没有#!行的情况下,脚本(可能)将用/bin/sh执行,但是没有明确的意义不大.

The #! line, known as a shebang, controls what shell is used to execute your script. The interactive shell you execute the script from is irrelevant. (It pretty much has to be that way; otherwise scripts would behave differently for different users). In the absence of a #! line, a script will (probably) be executed with /bin/sh, but there's not much point in not being explicit.

这篇关于在bash中回显打印-e的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆