0

I wrote a script named java_home.sh with the following content

export JAVA_HOME=/path/to/java/home

I ran ./java_home.sh on the command line, and then I did echo $JAVA_HOME - the script did not work, JAVA_HOME has not been set.

But copying the content of the script and typing on the terminal export JAVA_HOME=/path/to/java/home does the trick - the environment variable is added.

I'm confused - how is running the script directly different from copying out its content and run it myself? They produce different results.

P.S. I wrote the script with the intention of putting it under /etc/profile.d so that it can set the environment variable for all users when the system starts up, so it bothers me that the script is not working.

4
  • 1
    shells are different. to run in current shell . ./java_home.sh Commented Jul 21, 2020 at 5:27
  • 2
    You can't export variable in your shell script and get it in your current shell Commented Jul 21, 2020 at 5:29
  • @megamonium : The variable has been set - in the process which executes the script. You can verify it by doing inside java_home.sh a printenv JAVA_HOME. Of course it is not set then in your terminal. If you had sourced the script, hence avoided to create a separated process for it, you would have got your setting in your current shell. Commented Jul 21, 2020 at 6:07
  • unix.stackexchange.com/q/599445 Commented Jul 21, 2020 at 7:50

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.