makefile

Makefile does not create multiple txt-files using touch


I'm new to makefiles and trying to create multiple files at once by using a makefile containing touch file-{1..10}.txt. This doesn't work as expected as it creates a file called file-{1..10}.txt instead of creating the files file-1.txt through file-10.txt.

This is the code of the makefile:

   .DEFAULT_GOAL := generate

say_hello:
    @echo "Hello World"

generate:
    @echo "Creating empty text files..."
    touch file-{1..10}.txt

clean:
    @echo "Cleaning up..."
    rm *.txt

When entering make into the terminal, it prints

Creating empty text files...
touch file-{1..10}.txt

and creates the file file-{1..10}.txt.
Entering touch file-{1..10}.txt into the terminal creates file-1.txt through file-10.txt as usual.


Solution

  • The POSIX standard requires that make always invoke the shell /bin/sh. It will never invoke the user's preferred shell (think what a disaster of non-portability that would be!)

    Brace expansion such as you want is not part of the POSIX definition of sh. It's an add-on feature provided by bash and some other shells. On systems where /bin/sh is a link to bash, such as MacOS and Red Hat-based systems, brace expansion will work in makefiles.

    On systems where /bin/sh is a link to a POSIX-compliant shell like dash, such as Ubuntu, trying to use non-POSIX features in your makefile recipes will not work.

    You can either write out the touch operation, or you can add:

    SHELL := /bin/bash
    

    to your makefile to force make to invoke bash. Just be aware that your makefile is now no longer portable to systems that don't have /bin/bash installed.