简体   繁体   中英

Makefile: echo not properly printing

I have below command

$ echo \\newcommand{\\coverheight}{11.0in} > tmp
$ cat tmp
echo \\newcommand{\\coverheight}{11.0in} > tmp

But when I'm using same echo command in make file, it's not properly writing to file.

# Makefile
all:
       printf '\\newcommand{\\coverheight}{11.0in}' > tmp

After running `make', the output is:

$ cat tmp 

ewcommand{

How to write to a file properly using Makefile using echo ?

make just sends a recipe (except for splitting long lines) to your shell and do not interpret it. So it is your shell that interprets it.

So your shell run this echo and printf commands. And shells like bash or zsh use builtin commands for echo and printf (if you don't say to use the /bin/echo command explicitly).

And there is a difference between behaviour of builtin commands between shells. What is more is that you can use one shell for running interactive commands and make use different shell (by default /bin/sh) for handling recipies.

Here is an example of difference between shells. When I run in echo \\\\newcommand in bash I get:

$ echo \\newcommand
\newcommand

And when I run echo \\\\newcommand in zsh I get:

$ echo \\newcommand

ewcommand

I suspect that you get different result because of it. And actually printf '\\\\newcommand{\\\\coverheight}{11.0in}' must be more correct because it uses strong quoting.

Anyway one way to print in makefile seems to be using external command /bin/echo:

all:
       command echo '\\newcommand{\\coverheight}{11.0in}' > tmp

Or use strong quote as you already have done:

all:
       printf '\\newcommand{\\coverheight}{11.0in}' > tmp

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