Makefile: trailing spaces in a variable

I read the following in the GNU manual:

if you do not want any whitespace characters at the end of your variable value, you must remember not to put a random comment on the end of the line after some whitespace, such as this: dir := /foo/bar # directory to put the frobs in Here the value of the variable dir is '/foo/bar ' (with four trailing spaces), which was probably not the intention. (Imagine something like '$(dir)/file' with this definition!) 

I tried using a simple make file as shown below:

 foo := hi # four trailing spaces all: @echo $(foo)_ 

when doing 'make', the output is just "hi _", only one space between the "hello" and the underline. Why aren't there four spaces?

Thanks,

+4
source share
1 answer

When make runs this script, it does not pass the variable to the echo, but instead replaces $(foo) value foo .

Thus, the actual script is executed by echo hi...._ (points are being specified).

And spaces are simply ignored when parsing arguments for echo .

You can put double quotation marks so that it appears as a string.

 echo "$(foo)_" 
+3
source

Source: https://habr.com/ru/post/1445483/


All Articles