8

I have a variable in Makefile:

JAVABIN = $(shell dirname $(which java))

And when I echo the JAVA_HOME variable in Makefile, the variable definition complains:

dirname: missing operand
Try 'dirname --help' for more information.

When I quote the $(which java), the JAVABIN is ., so the result is wrong. And I didn't understand how make reads a Makefile, maybe it is the cause. Thank you very much.

zhenguoli
  • 2,268
  • 1
  • 14
  • 31

1 Answers1

16

You need to escape the dollar:

JAVABIN = $(shell dirname $$(which java))

See 6.1 Basics of Variable References.


The specific error message you received was caused by the fact that the $(which java) piece expanded to the empty string, since it was an undefined variable. Hence the dirname system command ended up seeing no arguments, in which case it complains of a "missing operand".

bgoldst
  • 34,190
  • 6
  • 38
  • 64