Bash – Is it possible to use bash parameter expansion in Makefile

bashmake

My Makefile:

FULL_VERSION ?= 1.2.3
MINOR_VERSION := $(shell echo "${FULL_VERSION%.*}")

test:
    echo $(MINOR_VERSION)

Running make test gives nothing, I want to get 1.2.

I know I can get it via sed/grep but I'm looking for a more elegant solution, seems there's nothing simpler than bash parameter expansion

Best Answer

You'd need to first store the value in a shell variable:

MINOR_VERSION := $(shell v='$(FULL_VERSION)'; echo "$${v%.*}")

(assuming $(FULL_VERSION) doesn't contain single quotes)

Now that calls sh, not bash. ${var%pattern} is a standard sh operator (comes from ksh).

If you wanted to use bash-specific operators, you'd need to tell make to call bash instead of sh with

SHELL = bash

Beware however that many systems don't have bash installed by default which would make your Makefile non-portable (but then, some systems don't have GNU make either and you're already using some GNUisms there)).

Related Question