rhz
rhz

Reputation: 1132

printing a variable in a makefile with multiple spaces

Consider the following simple makefile:

define HELP_MSG
  
Usage:\n
        make help -show this message\n
        make help                   -show spaces and then this message\n

endef
export HELP_MSG

help:
        @echo $$HELP_MSG

which outputs:

Usage:
 make help -show this message
 make help -show spaces and then this message

How can I have @echo respect the extra spacing on the second output line?

Upvotes: 0

Views: 204

Answers (2)

HARSH MITTAL
HARSH MITTAL

Reputation: 760

You can use -e along with echo and use tab for space. Eg:

define HELP_MSG
  
Usage:\n
        make help -show this message\n
        make help \t-show spaces and then this message\n

endef
export HELP_MSG

help:
        @echo -e $$HELP_MSG

To add custom spaces, use printf or " " in echo. Eg:

define HELP_MSG
  
Usage:\n
        make help -show this message\n
        make help           -show spaces and then this message\n

endef
export HELP_MSG

help:
    @echo -e "$$HELP_MSG"
    @printf '%s\n' "$$HELP_MSG"

Upvotes: 0

MadScientist
MadScientist

Reputation: 101081

There is no portable way to print formatted text with echo. The -e option to echo is not standardized and not all versions of echo support -e. You should never try to print anything other than simple text that you know does not begin with a dash (-). Basically, anything other than a simple static string.

For anything more complex, you should use printf.

Also, if you want to print non-trivial text you MUST quote it, otherwise the shell will interpret it and mess it up for you.

help:
        @printf '%s\n' "$$HELP_MSG"

Upvotes: 1

Related Questions