Using curly brackets for variable expansion in makefile doesn't work
Asked Answered
G

3

18

When I run below command on shell, it works properly. but if I write it in a Makefile and call it with "make" command it doesn't work.

cp wpa_{cli,supplicant,passphrase,event} /usr/local/bin/

error after "make" command:

cp: cannot stat `wpa_{cli,supplicant,passphrase,event}': No such file or directory

What can i do to make it work with Makefile? I use Ubuntu 12.04. Same Makefile works on other linux distributions.

Godfather answered 11/10, 2012 at 11:17 Comment(0)
B
32

Make uses old-school Bourne shell (/bin/sh) by default which does not support brace expansion. Set the SHELL variable in your makefile to /bin/bash if it's not already set.

Just add a line in the top of your makefile with:

SHELL=/usr/bin/bash

(please confirm your bash path).

Bekelja answered 11/10, 2012 at 11:39 Comment(1)
so we should add SHELL=/bin/bash as the first line?Parette
H
1

As Steve K explains, it's a bashism. If you want to stick more closely to the standard, you can use a for loop without too much trouble.

for x in cli supplicant passphrase event; do cp -v wpa_$$x /usr/local/bin/; done

As usual for makefiles, put it all on one line or use backslashes to break it into a few lines. The double dollar sign indicates that make needs to pass the $ to the shell.

Hemistich answered 6/12, 2019 at 18:0 Comment(0)
A
0

i like @bk 's answer since it stays within the conventions of the tool, but for reference, you could also directly call the bash shell for just one command to do the brace expansion:

wpa_files := $(shell /bin/bash -c 'echo wpa_{cli,supplicant,passphrase,event}')

target:
    cp $(wpa_files) /usr/local/bin/
Adulteress answered 11/2, 2021 at 21:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.