Score:1

GNU make executes weird shell

cn flag

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.

hr flag
Hmm... interesting. Based on `strace` output it seems not to use a shell at all (instead passing the command + args to `execve` directly), either when make variable `SHELL` is undefined **or** set to `/bin/sh`. It seems to be possible to trick it into using `sh` on systems where /bin and /usr/bin are symlinked by setting `SHELL=/usr/bin/sh`.
Jakub Klinkovský avatar
cn flag
Arch Linux also symlinks `/bin` to `/usr/bin` and it works just fine, with and without `SHELL := /bin/sh`. So what's the difference between using `SHELL := /bin/sh` and `SHELL := /usr/bin/sh` on Ubuntu?
Mei avatar
ca flag
Mei
In my Ubuntu 20.04.4 LTS system, `/bin/sh` is symlinked to *bash* and `/usr/bin/sh` does not exist. At one time, I think `/bin/sh` may have been symlinked to *dash* and `/usr/bin/sh` may have been *bash*. I'd have to confirm that.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.