Assign results of a bash command to a variable in make

I'm trying to get this shell script to work in make:

$ VERSION=$(echo 'ThisBuild / version := "1.20"' | grep -e 'This.*version' | grep -Eo '[0-9]+\.[0-9]+')
$ echo $VERSION
1.20

make:

$ make -v
GNU Make 4.2.1
Built for x86_64-pc-linux-gnu

Makefile:

version:

    VERSION=$(echo 'ThisBuild / version := "1.20"' | grep -e 'This.*version' | grep -Eo '[0-9]+\.[0-9]+') && \
    echo "VERSION: ${VERSION}"

Doesn't work:

$ make version
VERSION= && \
echo "VERSION: "
VERSION: 

Can this be done?


Solution 1:

If you want to pass $ to the shell you have to escape it from make, by doubling it: $$.

version:
        VERSION=$$(echo 'ThisBuild / version := "1.20"' | grep -e 'This.*version' | grep -Eo '[0-9]+\.[0-9]+') && \
        echo "VERSION: $${VERSION}"

Otherwise, make will think that you're referencing a make variable and expand it.