Writing Command Lines

Writing good, portable makefile files is a bit of an art. Skill comes with practice and experience. Here are some tips to get you started:

  • Depending upon your locale, naming your file Makefile instead of makefile can cause it to be listed first with ls. This makes it easier to find in a directory with many files.

  • Remember that command lines must start with a leading tab character. You cannot just indent the line with spaces, even eight spaces. If you use spaces, make exits with an unhelpful message about a “missing separator.”

  • Remember that $ is special to make. To get a literal $ into your command lines, use $$. This is particularly important if you want to access an environment variable that isn’t a make macro. Also, if you wish to use the shell’s $$ for the current process ID, you have to type it as $$$$.

  • Write multiline shell statements, such as shell conditionals and loops, with trailing semicolons and a trailing backslash:

        if [ -f specfile ] ; then \
        ... ; \
        else \
        ... ; \
        fi

    Note that the shell keywords then and else don’t need the semicolon. (What happens is that make passes the backslashes and the newlines to the shell. The escaped newlines are not syntactically important, so the semicolons are needed to separate the different parts of the command. This can be confusing. If you use a semicolon where you would normally put a newline in a shell script, things should work correctly.)

  • Remember that each line is run in a separate shell. This means that commands ...

Get Unix in a Nutshell, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.