3

I have written a script to find all running docker containers with a certain name and it works when I directly type it into my terminal but as soon as I put it a Makefile it throws an error

/bin/sh: 1: Bad substitution

This is the script in makefile:

remote: FORCE
   docker ps -q --filter name=$$(tmp=$${PWD##*/} && printf "%s_workspace" "$${tmp//./}")

To clarify what that chunk after name= is doing, it's trying to get the current folder name and remove all .'s and append it to my container name which is workspace.

Hirad Roshandel
  • 2,175
  • 5
  • 40
  • 63

1 Answers1

6

The substitution operator you are using isn't supported by /bin/sh. You need to tell make to use bash instead:

SHELL := /bin/bash

If you want to keep your recipe POSIX-compatible, use tr instead:

remote: FORCE
        docker ps -q --filter name=$$(printf '%s_workspace' "$${PWD##*/}" | tr -d .)

If you are using GNU make, you might want to use

remote: FORCE
        docker ps -q --filter name=$(subst .,,$(notdir $(PWD)))_workspace

instead to let make to all the string processing itself.

chepner
  • 497,756
  • 71
  • 530
  • 681