<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>makefile — Little Step</title>
    <link>https://rex.writeas.com/tag:makefile</link>
    <description></description>
    <pubDate>Tue, 17 Nov 2026 00:35:53 +0000</pubDate>
    <item>
      <title>Use shell commands in Makefile</title>
      <link>https://rex.writeas.com/use-shell-commands-in-makefile-j0df?pk_campaign=rss-feed</link>
      <description>I have several related packages in different git repositories, and each repository has several branches. It is a headache to package all ...</description>
      <content:encoded><![CDATA[<p>I have several related packages in different git repositories, and each repository has several branches. It is a headache to package all of these packages during release period.</p>

<p>So I decided to create a new repository including all of the packages, and package the single branch at one time.</p>

<p>The simple project structure, we have several packages in pkg directory, and each has its own build.sh.</p>

<p>Since it is very simple, don&#39;t want to just add one Makefile for the whole thing, the content is like:</p>

<pre><code class="language-shell">CWD:=$(shell pwd)
BUILD:=$(CWD)/build
PACKAGE_NAME:= app-$(shell date &#34;+%Y%m%d%H%M&#34;).tar.gz

package:
        for pkg in $(ls -1 $(CWD)/pkgs); do \
                echo &#34;### starting build package: $pkg...&#34;; \
                $(CWD)/pkgs/$pkg/build.sh  $(BUILD); \
                echo &#34;### finish build package $pkg&#34;; \
                echo ; \
        done
</code></pre>

<p>And after I run <strong>make</strong>, I got no success.</p>

<pre><code class="language-shell">for pkg in ; do \
        echo &#34;### starting build package: kg...&#34;; \
        /home/xxx/app/pkgs/kg/build.sh  /home/xxx/app/build; \
        echo &#34;### finish build package kg&#34;; \
        echo ; \
done
</code></pre>

<p>The <strong>ls</strong> command and <strong>$pkg</strong> are not executed/expanded correctly.</p>

<p>After some <a href="https://blog.melski.net/2010/11/15/shell-commands-in-gnu-make/" rel="nofollow ugc">search</a> I know that the shell commands in makefile may be invoked in one shell, and the statements will be expanded twice for shell commands, so we need <strong>double dollar</strong> signsfor shell variables.</p>

<pre><code class="language-log">Essentially, gmake scans the command-line for shell built-ins (like for and if) and “shell special characters” (like | and &amp;). If none of these are present in the command-line, gmake will avoid the overhead of the shell invocation by invoking the command directly (literally just using execve to run the command).
[...]
gmake expands command-lines before executing them.

Command expansion is why you can use gmake features like variables (eg, $@) and functions (eg, $(foreach)) in the recipe. It is also why you must use double dollar signs if you want to reference shell variables in your recipe...
</code></pre>

<p>The correct statement is:</p>

<pre><code class="language-shell">for pkg in $$(ls -1 $(CWD)/pkgs); do \
        echo &#34;### starting build package: $$pkg...&#34;; \
        $(CWD)/pkgs/$$pkg/build.sh  $(BUILD); \
        echo &#34;### finish build package $$pkg&#34;; \
        echo ; \
done
</code></pre>

<p>Since <strong>CWD</strong> and <strong>BUILD</strong> are variables in Makefile, so there are referenced with <strong>single</strong> dollar signs. And <strong>ls</strong> and <strong>pkg</strong> are variables in shell, theses variables are referenced with <strong>double</strong> dollar signs.</p>

<p>#shell <a href="https://rex.writeas.com/tag:makefile" class="hashtag" rel="nofollow ugc"><span>#</span><span class="p-category">makefile</span></a></p>
]]></content:encoded>
      <guid>https://rex.writeas.com/use-shell-commands-in-makefile-j0df</guid>
      <pubDate>Sun, 20 Jun 2021 06:44:52 +0000</pubDate>
    </item>
  </channel>
</rss>