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:


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

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

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

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

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:


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)
  • 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. – bertalanp99 Jan 3 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.