0

I am trying to remove the path prefix. Here is a small example showing just the issue.

Makefile

dist_directory = ./dist

default: build

build: $(patsubst %.md, $(dist_directory)/%.html, $(wildcard *.md))

$(dist_directory)/%.html: %.md
    @echo start
    @echo $@
    @echo ${$@//$(dist_directory)/}
    @echo end

Create a file: touch stuff.md

Then build: make

The output is:

start
dist/stuff.html

end

The expected output is:

start
dist/stuff.html
/stuff.html
end

There are similar posts on Stack Exchange. However, they have not worked for me in a Makefile for some reason. I'm probably doing something wrong.

https://unix.stackexchange.com/questions/311758/remove-specific-word-in-variable

Remove a fixed prefix/suffix from a string in Bash

Remove substring matching pattern both in the beginning and the end of the variable

101010
  • 14,866
  • 30
  • 95
  • 172

1 Answers1

0

You have many issues here. The most fundamental one is that if you want to use shell variables you have to escape the dollar sign so that make doesn't interpret it. And, you can only use shell variable substitutions on shell variables, while $@ is a make variable, so you need:

@foo='$@' ; echo $${foo//$(dist_directory)/}

The more subtle one is that make always uses /bin/sh (POSIX standard shell) when it invokes recipes, and the above syntax is specific to bash. One way around that would be to explicitly set SHELL := /bin/bash in your makefile to force make to use bash. Luckily that is not necessary because POSIX sh can also do this, as mentioned by Reda in another answer:

@foo='$@' ; echo $${@##*/}

But even more, you don't need any of this because make sets the automatic variable $* to the part of the target that matches the stem (the %):

@echo $*.html

It also sets $(@F) to the filename part of the $@ variable:

@echo $(@F)

ETA

If you want to do something very similar to your shell variable expansion using GNU make you can use:

@echo $(patsubst $(dist_directory)/%,%,$@)
MadScientist
  • 92,819
  • 9
  • 109
  • 136