Bash – Makefile looping files with pattern

bashmake

In bash, I can do this:

for name in sqls/*.{schema,migration}.sql; do
    echo $name; # some operation on the file name
done

It would output something like this:

sqls/0001.schema.sql
sqls/0002.schema.sql
sqls/0003.migration.sql
sqls/0004.schema.sql
sqls/0006.schema.sql
sqls/0007.schema.sql

However, if I put the same loop into a Makefile:

names:
    for name in sqls/*.{schema,migration}.sql; do echo $name; done

The output become:

sqls/*.{schema,migration}.sql

How can I do the same thing in a Makefile?

Best Answer

You need to realize that the {...} is a bash construct whilst Makefiles generally launch the sh which does not support the brace expansion feature.

However all's not lost; if you are with GNU Make then you can tell Make to use bash by putting this little line SHELL := bash at the very top of your Makefile.

Related Question