简体   繁体   English

ubuntu中的echo -e选项不起作用

[英]echo -e option in ubuntu doesn't work

My colleague use Ubuntu and I use openSUSE, we compiled same source code using same makefile, my environment works well, but my colleague can't, always output the can't recognized -e option. 我的同事使用Ubuntu我使用openSUSE,我们使用相同的makefile编译相同的源代码,我的环境运行良好,但我的同事不能,总是输出无法识别的-e选项。 We check the makefile, ONLY find echo command use -e option. 我们检查makefile,只找到echo命令使用-e选项。

Dose Ubuntu's echo function is different with others? 剂量Ubuntu的回声功能与其他功能不同?

+++++update in makefile, the echo define as: 在makefile中+++++更新,echo定义为:

TOOLSDIR =

ECHO = $(TOOLSDIR)echo -e

The $(TOOLSDIR) is dir for tool, this make file can detect compile env, if linux or has CYGWIN: $(TOOLSDIR)是工具的目录,这个make文件可以检测编译环境,如果linux或者有CYGWIN:

$(TOOLSDIR) is empty, if windows, it will goes to WIN32 version echo tool dir, like: $(TOOLSDIR)是空的,如果是windows,它会转到WIN32版本的echo工具目录,如:

TOOLSDIR = $(subst /,\\,$(MAKEDIR)/tools/WIN32/)

after we execute make, ubuntu will output: 在我们执行make之后,ubuntu将输出:

Fatal error: L3900U: Unrecognized option '-e'. 致命错误:L3900U:无法识别的选项'-e'。

however, openSUSE doesn't have this error 但是,openSUSE没有此错误

+++++update for echo -e ++++更新echo -e

I write a test makefile in ubuntu 我在ubuntu中编写了一个测试makefile

all:
    echo -e hello

it output: 它输出:

echo -e hello echo -e你好

hello 你好

+++++update makefile test in openSUE ( 12.1 ) and ubuntu ( 12.04 ) ++++更新makefile测试在openSUE( 12.1 )和ubuntu( 12.04

all:
    echo -e "hello 1"
    echo -e hello 2
    echo -e he\nllo3
    echo -e "he\nllo4"
  1. opensuse, the output is: opensuse,输出是:

echo -e "hello 1" echo -e“你好1”

hello 1 你好1

echo -e hello 2 echo -e你好2

hello 2 你好2

echo -e he\\nllo3 echo -e he \\ nllo3

henllo3 henllo3

echo -e "he\\nllo4" echo -e“he \\ nllo4”

he

llo4 llo4

  1. in ubuntu, the output is: 在ubuntu中,输出是:

echo -e "hello 1" echo -e“你好1”

-e hello 1 你好1

echo -e hello 2 echo -e你好2

hello 2 你好2

echo -e he\\nllo3 echo -e he \\ nllo3

henllo3 henllo3

echo -e "he\\nllo4" echo -e“he \\ nllo4”

-e he - 他

llo4 llo4

Meanwhile, i test in ubuntu, echo without -e , the result is same as in openSUSE, echo with -e 同时,我在ubuntu中测试,没有-e echo,结果与openSUSE相同,echo与-e

It could depend from the shell you guys are using (echo is often implemented inside the shell) 它可能取决于你们正在使用的shell(echo经常在shell中实现)

this is what happens in OS X (Mountain Lion): 这是OS X(Mountain Lion)中发生的事情:

$ bash
bash-4.2$ echo -e foo
foo
bash-4.2$ sh
sh-3.2$ echo -e foo
-e foo

The same for OpenSUSE, and Ubuntu. OpenSUSE和Ubuntu也一样。

-e option is not compliant to POSIX (I'm not sure, but it should be the default behavior), btw, to print formatted text, printf is more appropriate command. -e选项不符合POSIX(我不确定,但它应该是默认行为),顺便说一句,要打印格式化文本, printf是更合适的命令。

From the information you provide, it looks to me that your makefile has a bug (portability issue). 根据您提供的信息,我认为您的makefile存在错误(可移植性问题)。

On OSX man echo doesn't list the -e option too, btw. 在OSX上, man echo也没有列出-e选项,顺便说一句。 On Ubuntu 12.10 the -e is present on my computer, on my router BusyBox v1.13.4 also accepts the -e (it's internal command for busybox) 在Ubuntu 12.10上, -e出现在我的计算机上,在我的路由器上,BusyBox v1.13.4也接受-e (它是busybox的内部命令)

But probably it's just the /bin/sh using internal command and ignoring it. 但可能只是/bin/sh使用内部命令并忽略它。

==== UPDATE: ====更新:

In your makefile remove the $(TOOLSDIR) in front of echo: 在你的makefile中删除echo前面的$(TOOLSDIR):

ECHO = echo -e

in fact if you specify the path of the echo (ie /bin/echo ) you force the shell to execute the program from the filesystem instead of the one implemented internally by the shell: 实际上,如果指定echo的路径(即/bin/echo ),则强制shell从文件系统执行程序,而不是shell内部实现的程序:

$ echo -e foo
foo
$ /bin/echo -e foo
-e foo

I also find it may cause by ubuntu link /bin/sh to dash but NOT bash . 我也发现它可能会导致ubuntu link /bin/sh dash而不是bash After I use bash , problem solved and there is an other question related to this, which I asked: same shell script has different behaviou on different Linux distribution 在我使用bash ,问题解决了,还有一个与此相关的问题,我问: 相同的shell脚本在不同的Linux发行版上有不同的行为

Both Luigi R. Viggiano and How Chen provide good explanations to the problem and solutions to it. Luigi R. Viggiano和How Chen都对问题及其解决方案提供了很好的解释。 In fact, their answers complement each other. 事实上,他们的答案相互补充。

In dash , the echo command does not accept the -e flag. dash中echo命令不接受-e标志。 However, in bash , it does. 但是,在bash中 ,确实如此。

In Ubuntu, one might consider using update-alternatives to select bash , rather than dash , as the default shell. 在Ubuntu中,可以考虑使用update-alternatives选择bash而不是dash作为默认shell。 The following commands will do the trick (warning, these commands will apply system-wide): 以下命令将执行此操作(警告,这些命令将在系统范围内应用):

sudo update-alternatives --install /bin/sh sh /bin/bash 0
sudo update-alternatives --install /bin/sh sh /bin/dash 0
sudo update-alternatives --set sh /bin/bash

The \\ is the escape character in the shell, so \\n is just that, an "n", so escape the escape \\\\\\n . \\是shell中的转义字符,所以\\n只是一个“n”,所以逃避转义\\\\\\n

[sg@Study ~]$ echo -e \\nfoo\\nfoo

foo
foo
[sg@Study ~]$

or use quotes 或使用报价

[sg@Study ~]$ echo -e "\\nfoo\\nfoo"

foo
foo
[sg@Study ~]$ 

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

相关问题 shell - 临时 IFS 仅作为换行符。 为什么这不起作用: IFS=$(echo -e '\n') - shell - temp IFS as newline only. Why doesn't this work: IFS=$(echo -e '\n') 如果bash没有在双引号中保留反斜杠,那么为什么echo -e“\\ n”有效? - If bash doesn't preserve a backslash in double quotes, then why does echo -e “\n” work? echo smbpasswd by --stdin 不起作用 - echo smbpasswd by --stdin doesn't work at job scheduler 在我的 Ubuntu 上不起作用 - at job scheduler doesn't work on my Ubuntu 使用echo -e选项打印变量的值 - Printing value of variable with echo -e option 着色回声在Solaris中有效,但在Linux中不起作用 - Colorizing echo works in Solaris but doesn't work in Linux xargs echo `echo {} | sed 's/pattern/replace/'` 不起作用,但对于每个循环都有效 - xargs echo `echo {} | sed 's/pattern/replace/'` doesn't work but for each loop works GNU Parallel: - line-buffer选项不适用于--pipe - GNU Parallel: --line-buffer option doesn't work with --pipe 为什么不能在subprocess.Popen()或os.system()中回显> / dev / udp / ...? - Why doesn't echo > /dev/udp/… work from subprocess.Popen() or os.system()? BASH:grep 在 shell 脚本中不起作用,但 echo 显示正确的命令并且它在命令行上起作用 - BASH: grep doesn't work in shell script but echo shows correct command and it works on command line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM