Makefile `echo -n' not working

后端 未结 3 1292
你的背包
你的背包 2021-02-01 16:46

I am trying to have my Makefile echo text without the trailing new line, but am unable to. I am experiencing the behavior on OS X (on Linux everything works as expected).

3条回答
  •  北荒
    北荒 (楼主)
    2021-02-01 17:13

    Something about the quotes confuses make. Your code behaves the same for me, but the following works as expected:

    help:
            @echo -n Shouldn\'t print a newline
    

    Hardcoding the path to the executable also works:

    help:
            @/bin/echo -n "Shouldn't print a newline"
    

    The Mac OS X man page for echo, while discussing the existence of shell built-in echos, mentions that the echo of sh(1) does not support the -n option, but that fails to explain (to me, anyway) why my first alternative works.


    Confirmation that make is using sh to execute the commands by default. In

    SHELL = bash
    help:
            @echo -n "Shouldn't print a newline"
            @echo -n Shouldn\'t print a newline
    

    both echo statements behave the same (no newlines printed). So without that variable, we have bash pretending to be sh, but evaluating the two lines differently. Question 1: why? Question 2: is the second line the native bash echo or /bin/echo, rather than the emulated sh echo?

提交回复
热议问题