Bash extended globbing inside a Makefile

It would be more idiomatic and more portable to solve this with Make and not with Bash hacks. What if someone doesn't have Bash installed?

Anyway, here's the proper way to do it in Make:

FOOFILES = $(filter-out foo/bar/exclude_%,$(wildcard foo/bar/*))

target:
    for FILE in ${FOOFILES}; do echo $$FILE ; done

  1. You need to set extglob.
  2. You need to tell make to use bash, not sh.

Makefile:

SHELL=/bin/bash
.SHELLFLAGS="-O extglob -c"
 ...

Here is a simpler way:

SHELL:=/bin/bash -O globstar

which will enable globstar shell option (which works on BSD/OS X as well).

Tags:

Bash

Make