Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to preserve whitespace when saving command output to a Makefile variable?

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.

like image 897
sffc Avatar asked Jan 07 '19 01:01

sffc


1 Answers

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 |
                ||     ||
like image 67
Reinier Torenbeek Avatar answered Sep 26 '22 19:09

Reinier Torenbeek