Q:

Passing flags to make in bash script fails

I have a build script in bash which does something like:
if [[ $DEBUG ]]; then
    MAKE_CMD="$MAKE_CMD $DEBUG_FLAGS"
fi

$DEBUG_FLAGS are initialized to:
DEBUG_FLAGS="GDB_FLAG='-g' OTHER_CFLAGS='-O0 -g'"

then I just use $MAKE_CMD to compile the code. The script works without adding DEBUG_FLAGS to the original make command but adding the flags causes issues. It also works if I type in the $MAKE_CMD by hand with the same debug flags. The only thing I can deduce is that my quotation is wrong which is causing issues. Do you see something wrong with my quotations?

A:

make.sh:
#!/bin/bash

MAKE_CMD="make -f bash.mak"
DEBUG_FLAGS="GDB_FLAG='-g' OTHER_CFLAGS='-O0 -g'"

if [[ $DEBUG ]]; then
  MAKE_CMD="$MAKE_CMD $DEBUG_FLAGS"
fi

eval $MAKE_CMD

bash.mak: (mind the tabs vs spaces!)
all:
        echo $(GDB_FLAG)
        echo $(OTHER_CFLAGS)

Command line example:
$ DEBUG=true ./make.sh
echo -g
-g
echo -O0 -g
-O0 -g

