Search code examples
makefileescaping

Echo without interpreting newline


I have a makefile where one of the actions requires that I write a string \noslide to a textfile called modeset.txt, so I'm using 'echo'. Although the -E flag should disable interpretation of backslash escapes echo processes the flag as part of the sting (see output below).

Here is a stripped down snippet from my makefile:

.PHONY: target all

all: target

target:
    echo -E "\noslide" > modeset.txt
clean:
    rm -f modeset.txt

The content of the modeset.txt file shows that echo gobbled the -E as part of the string.

-E 
oslide

In a shell however, echo -e and echo -E work as expected, so what is the makefile environment doing to cause this?

OS: Vanilla Debian 8 from official sources only.


Solution

  • echo is not portable, beyond simple text. Many different versions exist that behave differently, and even within the same system it can depend on what shell you use (i.e., the shell's builtin echo vs the system's /bin/echo).

    If you want to display anything other than simple text followed by a newline, you should use printf not echo:

    target:
             printf '\\noslide\n' > modeset.txt