简体   繁体   中英

Interpolate variable in command substitution in Makefile

I am trying to do variable interpolation inside a command substitution in a Makefile.

I have this code:

setup:
  mkdir -p data_all ; \
  for i in $(shell jq -r 'keys | @tsv' assets.json) ; do \
    git_url=$(shell jq -r ".$$i" assets.json) ; \
    git clone $$git_url data_all/$$i ; \
  done

The code is failing, however, because $$i does not expand in the "shell" line that sets git_url.

How do I interpolate the variable $i in the "shell" line that sets git_url?

You mixed up make functions ( $(shell ...) ) and true shell constructs. When writing a recipe the simplest is to write it first in plain shell:

mkdir -p data_all ; \
for i in $( jq -r 'keys | @tsv' assets.json ) ; do \
    git_url=$( jq -r ".$i" assets.json ) ; \
    git clone $git_url data_all/$i ; \
done

And then escaping the unwanted $ expansion by make:

mkdir -p data_all ; \
for i in $$( jq -r 'keys | @tsv' assets.json ) ; do \
    git_url=$$( jq -r ".$$i" assets.json ) ; \
    git clone $$git_url data_all/$$i ; \
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM