Writing Shell Script Within Makefile

Tips for writing shell scripts within makefiles1

  1. Escape the script’s use of $ by replacing with $$
  2. Convert the script to work as a single line by inserting ; between commands
  3. If you want to write the script on multiple lines, escape end-of-line with \
  4. Optionally start with set -e to match make’s provision to abort on sub-command failure
  5. This is totally optional, but you could bracket the script with () or {} to emphasize the cohesiveness of a multiple line sequence – that this is not a typical makefile command sequence

Here’s an example inspired by the OP:

    { \
    set -e ;\
    msg="header:" ;\
    for i in $$(seq 1 3) ; do msg="$$msg pre_$${i}_post" ; done ;\
    msg="$$msg :footer" ;\
    echo msg=$$msg ;\

使用makefile创建hugo new post的处理:

        @if [ -z `echo $(TITLE)|sed -E -e 's/[[:blank:]]+/-/g'` ]; then\
           echo "TITLE not set. Pass in TITLE=<title name>"; exit 10;\

.PHONY: new
new: validate-create ## Create a new post in posts folder
        { \
        echo "== Creating new post";\
        output=$$(hugo new posts/`date -u +'%Y-%m-%d-'``echo $${TITLE}|sed -E -e 's/[[:blank:]]+/-/g'`.md 2>&1);\
        filename1=$$(echo "$$output"|awk '{print $$1}');\
        filename2=$$(echo "$$output"|awk '{print $$2}');\
        if [ -f "$$filename1" ]; then \
        elif [ -f "$$filename2" ]; then \
        else \
            echo "Something wrong";\
        typora "$$filename";\



  1. https://stackoverflow.com/questions/10121182/multi-line-bash-commands-in-makefile ↩︎