1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
<h1 class="subsection">Splitting Recipe Lines</h1> <p>One of the few ways in which <code>make</code> does interpret recipes is checking for a backslash just before the newline. As in normal makefile syntax, a single logical recipe line can be split into multiple physical lines in the makefile by placing a backslash before each newline. A sequence of lines like this is considered a single recipe line, and one instance of the shell will be invoked to run it. </p> <p>However, in contrast to how they are treated in other places in a makefile (see <a href="splitting-lines">Splitting Long Lines</a>), backslash/newline pairs are <em>not</em> removed from the recipe. Both the backslash and the newline characters are preserved and passed to the shell. How the backslash/newline is interpreted depends on your shell. If the first character of the next line after the backslash/newline is the recipe prefix character (a tab by default; see <a href="special-variables">Special Variables</a>), then that character (and only that character) is removed. Whitespace is never added to the recipe. </p> <p>For example, the recipe for the all target in this makefile: </p> <div class="example"> <pre class="example">all :
@echo no\
space
@echo no\
space
@echo one \
space
@echo one\
space
</pre>
</div> <p>consists of four separate shell commands where the output is: </p> <div class="example"> <pre class="example">nospace
nospace
one space
one space
</pre>
</div> <p>As a more complex example, this makefile: </p> <div class="example"> <pre class="example">all : ; @echo 'hello \
world' ; echo "hello \
world"
</pre>
</div> <p>will invoke one shell with a command of: </p> <div class="example"> <pre class="example">echo 'hello \
world' ; echo "hello \
world"
</pre>
</div> <p>which, according to shell quoting rules, will yield the following output: </p> <div class="example"> <pre class="example">hello \
world
hello world
</pre>
</div> <p>Notice how the backslash/newline pair was removed inside the string quoted with double quotes (<code>"…"</code>), but not from the string quoted with single quotes (<code>'…'</code>). This is the way the default shell (<samp>/bin/sh</samp>) handles backslash/newline pairs. If you specify a different shell in your makefiles it may treat them differently. </p> <p>Sometimes you want to split a long line inside of single quotes, but you don’t want the backslash/newline to appear in the quoted content. This is often the case when passing scripts to languages such as Perl, where extraneous backslashes inside the script can change its meaning or even be a syntax error. One simple way of handling this is to place the quoted string, or even the entire command, into a <code>make</code> variable then use the variable in the recipe. In this situation the newline quoting rules for makefiles will be used, and the backslash/newline will be removed. If we rewrite our example above using this method: </p> <div class="example"> <pre class="example">HELLO = 'hello \
world'
all : ; @echo $(HELLO)
</pre>
</div> <p>we will get output like this: </p> <div class="example"> <pre class="example">hello world
</pre>
</div> <p>If you like, you can also use target-specific variables (see <a href="target_002dspecific">Target-specific Variable Values</a>) to obtain a tighter correspondence between the variable and the recipe that uses it. </p><div class="_attribution">
<p class="_attribution-p">
Copyright © 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022 Free Software Foundation, Inc. <br>Licensed under the GNU Free Documentation License.<br>
<a href="https://www.gnu.org/software/make/manual/html_node/Splitting-Recipe-Lines.html" class="_attribution-link">https://www.gnu.org/software/make/manual/html_node/Splitting-Recipe-Lines.html</a>
</p>
</div>
|