Tony Stark Tony Stark - 1 year ago 100
Java Question

`echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.bash_profile`, what each part of the command actually mean/does?

to set JAVA_HOME on mac, one simply have to type

echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.bash_profile
in terminal. However, I was just wondering if anyone could kindly explain what each part of the command actually mean/does? Thank in advance for any help!

Answer Source

~/.bash_profile is a private, i.e. for a particular user, startup file for bash shell. In other words, when you open a shell (command line), the shell program reads this file to know what should be executed to initialize the shell environment.

JAVA_HOME is an environment variable used by various programs to find Java binaries and other Java-related files. You can set the variable by just executing export JAVA_HOME=$(/usr/libexec/java_home) in the command line, but it will be effective only during that shell session.

So, in order for the JAVA_HOME variable to persist between sessions, you set it automatically in ~/.bash_profile by appending the export command to this file using echo and redirection. echo prints a message, >> redirects the output, in this case into the ~/.bash_profile file. Notice the double-greater-than sign (>>) -- it means append, as opposed to >, which means overwrite.

In order to treat the argument of the echo command "as is" without interpreting it, the argument is enclosed in double-quotes and the dollar sign is escaped (i.e. \$).

To sum up, you should see the following line at the bottom of your ~/.bash_profile file after executing the command (run cat ~/.bash_profile to view the file's contents):

export JAVA_HOME=$(/usr/libexec/java_home)

And $(command) syntax is used for command substitution, that is /usr/libexec/java_home will be executed and its output assigned to JAVA_HOME.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download