Hikmat G.
Hikmat G.

Reputation: 2621

Different java version in .sh file and terminal MAC

I'm trying to set JAVA_HOME in macOS 10.14. Currently there are 2 jdk versions (jdk-11.0.8.jdk, jdk-14.0.2.jdk) installed in /Library/Java/JavaVirtualMachines and I've exported env variable in .bash_profile

export JAVA_HOME=`/usr/libexec/java_home -v 11`

In terminal all is ok. java -version prints 11.0.8, echo $JAVA_HOME shows 11's directory. But in ~/test.sh file java -version prints 14.0.2, $JAVA_HOME is empty. I tried to set env var in etc/profile but no success. Does anyone know what could possibly cause this?

Upvotes: 0

Views: 256

Answers (1)

Kurtis Rader
Kurtis Rader

Reputation: 7459

Welcome to the wonderful world of POSIX semi-compatible shells :-)

From the bash man page:

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and exe- cutes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.

Note that simply running a script does not execute the ~/.bash_profile script as the script is neither interactive nor a "login" shell. One solution is to set the BASH_ENV var to an appropriate initialization script.

Upvotes: 1

Related Questions