sh 中的“${x%% *}”是什么意思?

sh 中的“${x%% *}”是什么意思?

我刚刚在makefile中看到“$${x%% *}”,这意味着sh中的“${x%% *}”。为什么这么写呢?

makefile 如何检测命令在本地计算机中是否可用?

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

答案1

这是 POSIX shell 变量替换功能:

${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.

因此,如果var="abc def ghi jkl"

echo "${var% *}" # will echo "abc def ghi"
echo "${var%% *}" # will echo "abc"

相关内容