0

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?

2 Answers 2

1

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"
Sign up to request clarification or add additional context in comments.

Comments

0

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"

2 Comments

OK. This enabled me to print tabs. What if I wanted to print some arbitrary number of spaces?
@rhz I have edited the answer for an arbitrary number of spaces.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.