Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining local variable in Makefile target

How to define local variable in Makefile target?

I would like to avoid repeating filename like:

zsh:
    FILENAME := "text.txt"
    @echo "Copying ${FILENAME}...";
    scp "${FILENAME}" "user@host:/home/user/${FILENAME}"

But I am getting an error:

FILENAME := "text.txt"
/bin/sh: FILENAME: command not found

Same with $(FILENAME)

Trying

zsh:
    export FILENAME="text.txt"
    @echo "Copying ${FILENAME} to $(EC2)";

Gives me an empty value:

Copying ...
like image 638
Andrii Abramov Avatar asked Jun 06 '26 10:06

Andrii Abramov


1 Answers

You can't define a make variable inside a recipe. Recipes are run in the shell and must use shell syntax.

If you want to define a make variable, define it outside of a recipe, like this:

FILENAME := text.txt
zsh:
        @echo "Copying ${FILENAME}...";
        scp "${FILENAME}" "user@host:/home/user/${FILENAME}"

Note, it's virtually never correct to add quotes around a value when assigning it to a make variable. Make doesn't care about quotes (in variable values or expansion) and doesn't treat them specially in any way.

like image 136
MadScientist Avatar answered Jun 10 '26 20:06

MadScientist



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!