makefileglobrm

rm command works in shell but not in Makefile


I have an rm command that does not work in a Makefile but when I copy it to the shell it works:

$ make clean
rm paper.{abs,blg,pdf,aux,log,bbl,out,xmpdata}
rm: cannot remove 'paper.{abs,blg,pdf,aux,log,bbl,out,xmpdata}': No such file or directory
make: *** [Makefile:8: clean] Error 1
$ rm paper.{abs,blg,pdf,aux,log,bbl,out,xmpdata}
$

What can cause this? The file rights seem to be OK. I tried using double quotes and apostrophes around the filename expression but neither worked.

UPDATE: I run GNU Make 4.3 on Ubuntu 22.04


Solution

  • GNU Make runs the POSIX-compliant /bin/sh for a shell. It would be a disaster for portability if it invoked whatever shell the user was using.

    The {} form you're using is not a POSIX-conforming syntax: POSIX does not define any special behavior for {}. You are trying to take advantage of a capability of your personal shell (likely bash) which is non-standard.

    You either need to write out the multiple filenames or use standard POSIX globbing, or else add a line to your makefile to force it to use bash instead of /bin/sh, like:

    SHELL := /bin/bash
    

    Note then your makefile will not work properly on any system where /bin/bash is not present.

    You could also use a GNU Make function, such as:

    clean:
            rm $(addprefix paper.,abs,blg,pdf,aux,log,bbl,out,xmpdata)