views:

82

answers:

6

Recently I had to update my JAVA environment variable in .bashrc

echo $JAVA_HOME # prints out /usr/java/...

echo $(JAVA_HOME) # raises error "can't find JAVA_HOME command"

I'm worried that my make file, which uses $(JAVA_HOME) won't work since $JAVA_HOME gets recognized, but not $(JAVA_HOME)

How can I get $(JAVA_HOME) to equal $JAVA_HOME, which is currently set? Also, why does this happen?

thanks

+6  A: 

make is not bash

They deal with variables differently. $foo is how you read a variable called foo in bash, and $(foo) is how you read it in a makefile.

David Dorward
+1  A: 

make will set its variables to the environment. (the inverse is not true.)

sreservoir
+1  A: 

The reason for the difference between $JAVA_HOME and $(JAVA_HOME) is simply that make has a different syntax for variables than bash.

Peter Tillemans
+3  A: 

More precisely:

  • JAVA_HOME is a shell variable; assuming it has been exported with export, it is then an environment variable.
  • $JAVA_HOME is the bash syntax to expand the shell variable named JAVA_HOME.
  • In bash, $(command) substitutes the output of command -- so $(JAVA_HOME) is trying to run a command called JAVA_HOME, hence the error you got.
  • When make starts up, it looks at each environment variable and sets a make variable of the same name to the same value. (See http://www.gnu.org/software/make/manual/make.html#Environment .)
  • $(JAVA_HOME) is the make syntax to expand the make variable named JAVA_HOME.

So your Makefile should work just fine: $(JAVA_HOME) expands the make variable JAVA_HOME, which was set to the value of the environment variable JAVA_HOME (unless your Makefile deliberately did something to override it), which has the right value.

Matthew Slattery
A: 

... and ${foo} or ${HOME} is allowed too in bash, but, for HOME, seldom needed. It is useful for catenation, because ${HOME}OFFICE will expand to /home/stefanOFFICE on my machine, but I don't need it, while $HOMEOFFICE is completely unknown; $HOME isn't expanded.

$HOME is often concatenated, but with a slash, which is sufficient, to delimit the variable, so ${HOME}/bin and $HOME/bin work both.

user unknown
+1  A: 

Q: Will my make file read environment variables then? If I define $JAVA_HOME in .bashrc, will my make file read $(JAVA_HOME) correctly?

A: Yes, absolutely :)