I am aware of questions like this one, but my question is specifically about makefile, specifically gnumake.
I have a command that prints line breaks and other whitespace to standard out. I want to capture that output into a variable and then later print the variable to a file.
Example Makefile:
OUTPUT=${shell cowsay hello}
all:
@echo "$(OUTPUT)" > output.txt
After running make, output.txt contains:
_______ < hello > ------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
I want it to retain the whitespaces and instead contain:
_______
< hello >
-------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
The command I'm actually using is not cowsay, but it outputs similar whitespace and linebreaks.
I want to capture that output into a variable and then later print the variable to a file.
There does not seem to be a way around make
's mechanism to translate newlines in the shell command output into spaces. A bit of a hack that stays close to your original approach would be to have the shell convert newlines into some uncommon character (like \1
) when assigning the output to the variable and then have it translate it back when echo
-ing that variable to the file. Something like this:
OUTPUT=$(shell cowsay hello | tr '\n' '\1')
all:
@echo "$(OUTPUT)" | tr '\1' '\n' > output.txt
For me, this results in
$ make
$ cat output.txt
_______
< hello >
-------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With