Gnu Make Executes Weird Shell

Gnu Make Executes Weird Shell

The shell function in GNU make behaves weird on Ubuntu. Consider this simple Makefile:

TEST1 := $(shell command -v sh)
TEST2 := $(shell /bin/sh -c 'command -v sh')
all:
        $(info TEST1 = $(TEST1))
        $(info TEST2 = $(TEST2))

Running make prints

make: command: Command not found
TEST1 = 
TEST2 = /usr/bin/sh

Weird. Why does the default shell not know the command built-in? And what is the default shell invoked by GNU make actually? According to the GNU make manual:

The program used as the shell is taken from the variable SHELL. If this variable is not set in your makefile, the program /bin/sh is used as the shell.

There is no SHELL variable in the Makefile above, so the shell should be /bin/sh. However, running /bin/sh explicitly from the default shell actually behaves as expected, so there must be some difference.

So what is the real problem? It seems to be specific to GNU make packaged in Ubuntu, since the aforementioned Makefile behaves as expected on other systems like Arch Linux (which symlinks /bin/sh to bash rather than dash, though), i.e. it prints TEST1 = /usr/bin/sh.

2
Marcus Vance
Author

Marcus Vance

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.