Next: , Previous: Comments in Make Rules, Up: Portable Make

12.12 Newlines in Make Rules

In shell scripts, newlines can be used inside string literals. But in the shell statements of Makefile rules, this is not possible: A newline not preceded by a backslash is a separator between shell statements. Whereas a newline that is preceded by a backslash becomes part of the shell statement according to POSIX, but gets replaced, together with the backslash that precedes it, by a space in GNU make 3.80 and older. So, how can a newline be used in a string literal?

The trick is to set up a shell variable that contains a newline:

     nlinit=`echo 'nl="'; echo '"'`; eval "$$nlinit"

For example, in order to create a multiline ‘sed’ expression that inserts a blank line after every line of a file, this code can be used:

     nlinit=`echo 'nl="'; echo '"'`; eval "$$nlinit"; \
     sed -e "s/\$$/\\$${nl}/" < input > output