4

I just saw "$${x%% *}" in a makefile, which means "${x%% *}" in sh. Why it is written in this way ?

how can a makefile detect whether a command is available in the local machine?

determine_sum = \
        sum=; \
        for x in sha1sum sha1 shasum 'openssl dgst -sha1'; do \
          if type "$${x%% *}" >/dev/null 2>/dev/null; then sum=$$x; break; fi; \
        done; \
        if [ -z "$$sum" ]; then echo 1>&2 "Unable to find a SHA1 utility"; exit 2; fi

checksums.dat: FORCE
    $(determine_sum); \
    $$sum *.org
Galaxy
  • 225
  • 1
  • 2
  • 6

1 Answers1

9

It's a POSIX shell variable substitution feature :

${var%Pattern} Remove from $var the shortest part of $Pattern that matches the back end of $var.
${var%%Pattern} Remove from $var the longest part of $Pattern that matches the back end of $var.

So if var="abc def ghi jkl"

echo "${var% *}" # will echo "abc def ghi"
echo "${var%% *}" # will echo "abc"
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
frntn
  • 338
  • 2
  • 5
  • 2
    Not just `bash`, specified by POSIX `sh`. See [Parameter Expansion](http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02) in Shell Command Language. – manatwork May 07 '13 at 08:34