How can I use Bash syntax in Makefile targets? How can I use Bash syntax in Makefile targets? bash bash

How can I use Bash syntax in Makefile targets?


From the GNU Make documentation,

5.3.1 Choosing the Shell------------------------The program used as the shell is taken from the variable `SHELL'.  Ifthis variable is not set in your makefile, the program `/bin/sh' isused 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 ba:    @echo "a is $$0"b: SHELL:=/bin/bash   # HERE: this is setting the shell for b onlyb:    @echo "b is $$0"

That'll print:

a is /bin/shb 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.


You can call bash directly, use the -c flag:

bash -c "diff <(sort file1) <(sort file2) > $@"

Of course, you may not be able to redirect to the variable $@, but when I tried to do this, I got -bash: $@: ambiguous redirect as an error message, so you may want to look into that before you get too into this (though I'm using bash 3.2.something, so maybe yours works differently).


If portability is important you may not want to depend on a specific shell in your Makefile. Not all environments have bash available.