make: Splitting Recipe Lines
1
1 5.1.1 Splitting Recipe Lines
1 ----------------------------
1
1 One of the few ways in which 'make' does interpret recipes is checking
1 for a backslash just before the newline. As in normal makefile syntax,
1 a single logical recipe line can be split into multiple physical lines
1 in the makefile by placing a backslash before each newline. A sequence
1 of lines like this is considered a single recipe line, and one instance
1 of the shell will be invoked to run it.
1
1 However, in contrast to how they are treated in other places in a
1 makefile (⇒Splitting Long Lines Splitting Lines.),
1 backslash/newline pairs are _not_ removed from the recipe. Both the
1 backslash and the newline characters are preserved and passed to the
1 shell. How the backslash/newline is interpreted depends on your shell.
1 If the first character of the next line after the backslash/newline is
11 the recipe prefix character (a tab by default; ⇒Special
Variables), then that character (and only that character) is removed.
1 Whitespace is never added to the recipe.
1
1 For example, the recipe for the all target in this makefile:
1
1 all :
1 @echo no\
1 space
1 @echo no\
1 space
1 @echo one \
1 space
1 @echo one\
1 space
1
1 consists of four separate shell commands where the output is:
1
1 nospace
1 nospace
1 one space
1 one space
1
1 As a more complex example, this makefile:
1
1 all : ; @echo 'hello \
1 world' ; echo "hello \
1 world"
1
1 will invoke one shell with a command of:
1
1 echo 'hello \
1 world' ; echo "hello \
1 world"
1
1 which, according to shell quoting rules, will yield the following
1 output:
1
1 hello \
1 world
1 hello world
1
1 Notice how the backslash/newline pair was removed inside the string
1 quoted with double quotes ('"..."'), but not from the string quoted with
1 single quotes (''...''). This is the way the default shell ('/bin/sh')
1 handles backslash/newline pairs. If you specify a different shell in
1 your makefiles it may treat them differently.
1
1 Sometimes you want to split a long line inside of single quotes, but
1 you don't want the backslash/newline to appear in the quoted content.
1 This is often the case when passing scripts to languages such as Perl,
1 where extraneous backslashes inside the script can change its meaning or
1 even be a syntax error. One simple way of handling this is to place the
1 quoted string, or even the entire command, into a 'make' variable then
1 use the variable in the recipe. In this situation the newline quoting
1 rules for makefiles will be used, and the backslash/newline will be
1 removed. If we rewrite our example above using this method:
1
1 HELLO = 'hello \
1 world'
1
1 all : ; @echo $(HELLO)
1
1 we will get output like this:
1
1 hello world
1
11 If you like, you can also use target-specific variables (⇒
Target-specific Variable Values Target-specific.) to obtain a tighter
1 correspondence between the variable and the recipe that uses it.
1