简体   繁体   中英

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. We check the makefile, ONLY find echo command use -e option.

Dose Ubuntu's echo function is different with others?

+++++update in makefile, the echo define as:

TOOLSDIR =

ECHO = $(TOOLSDIR)echo -e

The $(TOOLSDIR) is dir for tool, this make file can detect compile env, if linux or has CYGWIN:

$(TOOLSDIR) is empty, if windows, it will goes to WIN32 version echo tool dir, like:

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

after we execute make, ubuntu will output:

Fatal error: L3900U: Unrecognized option '-e'.

however, openSUSE doesn't have this error

+++++update for echo -e

I write a test makefile in ubuntu

all:
    echo -e hello

it output:

echo -e hello

hello

+++++update makefile test in openSUE ( 12.1 ) and 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:

echo -e "hello 1"

hello 1

echo -e hello 2

hello 2

echo -e he\\nllo3

henllo3

echo -e "he\\nllo4"

he

llo4

  1. in ubuntu, the output is:

echo -e "hello 1"

-e hello 1

echo -e hello 2

hello 2

echo -e he\\nllo3

henllo3

echo -e "he\\nllo4"

-e he

llo4

Meanwhile, i test in ubuntu, echo without -e , the result is same as in openSUSE, echo with -e

It could depend from the shell you guys are using (echo is often implemented inside the shell)

this is what happens in 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.

-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.

From the information you provide, it looks to me that your makefile has a bug (portability issue).

On OSX man echo doesn't list the -e option too, btw. 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)

But probably it's just the /bin/sh using internal command and ignoring it.

==== UPDATE:

In your makefile remove the $(TOOLSDIR) in front of echo:

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 -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 . 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

Both Luigi R. Viggiano and How Chen provide good explanations to the problem and solutions to it. In fact, their answers complement each other.

In dash , the echo command does not accept the -e flag. However, in bash , it does.

In Ubuntu, one might consider using update-alternatives to select bash , rather than dash , as the default 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 .

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

foo
foo
[sg@Study ~]$

or use quotes

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

foo
foo
[sg@Study ~]$ 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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