2

This question already has an answer here:

I see many online tutorials for ubuntu, and some of them are using export= in the beginning of the script for system variables.

for example:

export JAVA_HOME=/usr/local/java/jdk1.8.0_05
export CATALINA_HOME=/ubuntuland/utils/apache-tomcat-8.0.8

See this tutorial

I am trying to understand the logic - as it makes sense to me that they will use system variables and not define their owns.

EDIT

when I write: echo $JAVA_HOME in the shell I see the correct content, in any shell, at any time of the server (even after reboot)

marked as duplicate by Cyrus, user79743, Anthon, don_crissti, garethTheRed Feb 28 '16 at 20:37

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • possible duplicate of stackoverflow.com/a/7328289/2203750 – Corleone Feb 28 '16 at 15:57
  • 2
    @Corleone questions on other SE sites don't count as duplicates. They can be cross-posts if they have been asked by the same person but not duplicates. – terdon Feb 28 '16 at 15:57
2

Sometimes you have dependencies of the version to be used to run something. In your case that would be a Java version to power your Tomcat server. The global Java version can change over time and make your Tomcat server crash due to bugs or incompatibilities. So if you define your own JAVA_HOME you can make sure that your Tomcat starts with a Java version that you earlier tested and you can rely on. Another point could be that you have several instances running and you need to specify a different CATALINA_HOME for the different instances.

I think it is even best practice to define the variables explicit instead of relying on variables that could change over time or even not exist, depending on the system wide configuration. This makes it also easier to troubleshoot things and to see the paths and binaries used.

1

The bash manual has this to say about export:

export [-fn] [name[=word]] ...
export -p
        The  supplied names are marked for automatic export to the envi‐
        ronment of subsequently executed commands. [...]

What this means is that when a variable is exported, it will be made available to any child process of the shell that exported it. To illustrate:

$ foo="bar"              
$ bash -c 'echo "$foo"'  ## the variable isn't available to the child

$ export foo="bar"
$ bash -c 'echo "$foo"' ## the child also gets the variable
bar

So, use export when you want to make a variable available to all processes and not only the currently running shell.

  • see my edited question - the variable is already set in my system – Dejel Feb 28 '16 at 15:58
  • @Dejel well yes, presumably, the lines you show are from one of the files that are read when the shell starts. I don't see how that's relevant. I may have misunderstood your question. What exactly is it that is confusing you? – terdon Feb 28 '16 at 16:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.