landau landau - 1 year ago 92
Bash Question

Customize SHELL in Makefile (set

I would like to customize the shell for a Makefile, but I am running into trouble. Here is a MWE. I have a Makefile,


all: abc def

touch abc

touch def

rm -f abc def

and a simple custom script,

eval $*

I made sure to run
chmod +x
beforehand. Then, when I typed
, I got an error.

make[3]: Entering directory `<CONFIDENTIAL>'
touch abc
./ line 2: eval: -c: invalid option
eval: usage: eval [arg ...]
make[3]: *** [abc] Error 2
make[3]: Leaving directory `<CONFIDENTIAL>`

I have been struggling to get rid of the
option. Setting
doesn't seem to work.

Answer Source

First, if you want to set a make variable to empty use


By adding the quotes you've actually set the variable to the literal string '' (make is not the shell and does not do shell quote stripping).

Second, the .SHELLFLAGS variable was added in GNU make 3.82 so if your version is older than that, it won't work. If you have 3.82 or newer then it does work (I just tried it).

Lastly, as Jean-Fran├žois points out you will problems if your command has escaped spaces. However, his solution of using "$@" cannot work as-is, because then the entire command is seen as a single string.

A more reliable implementation of the script would be:

exec /bin/bash "$@"

And then do not modify .SHELLFLAGS. Or else, do set .SHELLFLAGS to empty and make your script:

exec /bin/bash -c "$@"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download