开发者

Linux - environment variables $HOME versus $(HOME)

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 h开发者_如何转开发appen?

thanks


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.


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.


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 :)


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


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


... 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜