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.

megamonium
  • 463
  • 3
  • 7
  • 1
    shells are different. to run in current shell `. ./java_home.sh` – alecxs Jul 21 '20 at 05:27
  • 2
    You can't export variable in your shell script and get it in your current shell – mchelabi Jul 21 '20 at 05: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. – user1934428 Jul 21 '20 at 06:07
  • https://unix.stackexchange.com/q/599445 – alecxs Jul 21 '20 at 07:50

0 Answers0