Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading