Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

/bin/sh: 1: Bad substitution Makefile [duplicate]

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.

like image 456
Hirad Roshandel Avatar asked Oct 28 '25 19:10

Hirad Roshandel


1 Answers

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.

like image 107
chepner Avatar answered Oct 30 '25 09:10

chepner