Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 213
How Chen Avatar asked Jan 04 '13 01:01

How Chen


1 Answers

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 (i.e. /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
like image 155
Luigi R. Viggiano Avatar answered Oct 15 '22 04:10

Luigi R. Viggiano