How can I use Bash syntax in Makefile targets?

From the GNU Make documentation,

5.3.2 Choosing the Shell
------------------------

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.

So put SHELL := /bin/bash at the top of your makefile, and you should be good to go.

BTW: You can also do this for one target, at least for GNU Make. Each target can have its own variable assignments, like this:

all: a b

a:
    @echo "a is $$0"

b: SHELL:=/bin/bash   # HERE: this is setting the shell for b only
b:
    @echo "b is $$0"

That’ll print:

a is /bin/sh
b is /bin/bash

See “Target-specific Variable Values” in the documentation for more details. That line can go anywhere in the Makefile, it doesn’t have to be immediately before the target.

Leave a Comment