1

Today I'm learning make command, and I find it seems can execute any bash command by reading Makefile on current directory.

However, I encountered a problem. That when using variables, it seems that system will only read the first character of the variable.

Below is my file and running result:

# FILE CONTENT
Z="zen_on_the_moon"
now=$(date)

fun:
    touch $Z
    echo $now
    echo "file created on" $now >> $Z

# RUNNING IT
=>make fun
touch "zen_on_the_moon"
echo ow
ow
echo "file created on" ow >> "zen_on_the_moon"

How should I use the variable now in the Makefile under fun item?

1
  • makefile syntax seems to fit perfectly on a unix topic (as would bash sed or awk) . I see no reason to close.
    – Archemar
    Commented Oct 13, 2015 at 7:17

1 Answer 1

6

In Makefile, you refer to a variable by using syntax $(var_name). Using $var_name cause the first character other than a dollar sign $, open parenthesis ( or open brace { treated as variable name.

In $now, you actually get content of variable $n followed by literal string ow.

So you need:

$(now)

to get the content of variable named now.

Also note that now=$(date) get the content of variable named date instead of result of command date. You need to use shell function:

now=$(shell date)
1
  • Please note that this answer applies to GNU make. You can actually use braces ({ and }) instead of parentheses (( and )) to refer to variables (this is mentioned in the manual linked). In other make implementations, there is not always a shell function. For example, in FreeBSD or OpenBSD make, you would use FOO != some command to set a variable to the output of a shell command.
    – bp99
    Commented Jan 3, 2019 at 12:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .