Bash – Makefile not accepting conditionals

bashmake

I am using the following conditional statement within a Makefile:

mytarget:
    if [ -z "${TAG1}" | -z "${TAG2}" | -z "${TAG3}" ]
        then
        echo "Need to set all tag names images
        exit 1
    fi

but then …

$ make mytarget TAG1=latest TAG2=latest TAG3=latest
if [ -z "latest" | -z "latest" | -z "latest" ]
/bin/bash: -c: line 1: syntax error: unexpected end of file
Makefile:36: recipe for target 'env' failed
make: *** [env] Error 1

Best Answer

You need to have backslashes at the end of each (but the last) command line.

make sends each command line to a separate shell using: /bin/sh -ce "cmdline"

Note that since you don't have the newlines in the shell anymore, you may need to add a semicolon at before the backslash newline for some commands, e.g.

target:
    if true; \
        then \
            echo true;\
    fi

the backslash causes that make converts all these virtual lines to:

if true; then echo true; fi

before sending it to /bin/sh -ce cmd.

Related Question