help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: The strange echo command in makefile, may due to bash 4. Please help


From: Paul Smith
Subject: Re: The strange echo command in makefile, may due to bash 4. Please help.
Date: Fri, 18 Dec 2009 02:52:00 -0500

On Fri, 2009-12-18 at 14:33 +0800, "Chen Jun (陈军)" wrote:
> Hi, everyone. I run into a problem today. I have a very simple
> makefile:
> 
> -------------
> all:
>     @echo -e "line1\n"
> -------------
> 
> On openSUSE 11.1, it outputs
> 
> line1
> 
> But on Ubuntu 9.10, it outputs
> 
> -e line1
> 
> If I execute   echo -e "line1";  directly on Bash command line of both
> Linux, they both outputs "line1" only. In this case, I know /bin/echo
> is actually executed.
> 
> The difference between the two Linux I can speculate is the Bash 
> version: SUSE has bash 3.2.39, while Ubuntu has 4.0.33 . And GNU make 
> 3.81 try to use bash'es internal echo command in the  above makefile
> sample.

There are various differences.  First, remember GNU make always
run /bin/sh, it doesn't run /bin/bash.  On opensuse and Red Hat,
etc., /bin/sh is really bash.

On Ubuntu, /bin/sh is really dash which is a POSIX-compliant shell
without any of the fancy bash bells and whistles.  The builtin "echo"
command in dash adheres strictly to the POSIX spec, without any extra
bash or GNU features like "-e" support.

The next thing is that make is not invoking the shell for this (I don't
think).  Make has a "fast path" where, if it can see that the recipe is
sufficiently simple, make won't invoke a shell; instead it will parse
the command line and use fork/exec directly.  In that case you'll be
using the executable "echo" and not the builtin "echo".

> So, I'd like to always use external echo in my makefile, but don't let
> me change every occurrence of "echo" to "/bin/echo". Can somebody
> help?

You should just use a portable method, then you won't have to worry
about whether you're using the system echo, or the building shell echo,
and which shell you're using.  That's far too painful and error-prone to
be workable.  If you need to do processing that requires backslash
escape-type fanciness, you should use printf instead:

        all:
                @printf 'line1\n'

-- 
-------------------------------------------------------------------------------
 Paul D. Smith <address@hidden>          Find some GNU make tips at:
 http://www.gnu.org                      http://make.mad-scientist.net
 "Please remain calm...I may be mad, but I am a professional." --Mad Scientist





reply via email to

[Prev in Thread] Current Thread [Next in Thread]