3

There's a bash script on my linux machine with these two lines:

[ -r /etc/java/java.conf ] && . /etc/java/java.conf
export JAVA_HOME

What does the export JAVA_HOME do? Usually I thought export VARIABLE_NAME=something sets the variable to something.

What does running export JAVA_HOME without setting it to something do?

I tried running it on the commandline, but nothing happens.

leontp587
  • 791
  • 2
  • 9
  • 21
  • Have you tried reading [the documentation](https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html#Bourne-Shell-Builtins)? – melpomene Jun 17 '16 at 20:02
  • To be fair the documentation that you linked doesn't really address what the OP has asked - @melpomene – Jonny Henly Jun 17 '16 at 20:07
  • 1
    @JonnyHenly "*Mark each name to be passed to child processes in the environment.*" - That's what `export` does. – melpomene Jun 17 '16 at 20:09
  • Not an exact duplicate, but possibly still helpful: http://stackoverflow.com/questions/1158091/defining-a-variable-with-or-without-export – melpomene Jun 17 '16 at 20:16
  • @melpomene right - but you can see where someone might be confused by `export VAR_NAME` when they're used to seeing `export VAR_NAME=foo` or `VAR_NAME=foo; export VAR_NAME`. – Jonny Henly Jun 17 '16 at 20:26
  • Here's a question: Do you know what `foo=bar` does, and how that differs from `export foo=bar`? If you think that all variable assignments in shell require `export`, then that requires more background to answer. – Charles Duffy Jun 17 '16 at 20:36
  • 1
    ...btw, re: `export VARIABLE_NAME=something` -- all-caps names are reserved for variables that impact how tools defined by the shell or OS work; you don't want to use them for your own names. See fourth paragraph of the relevant POSIX definition at http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html -- granted, that document is only explicitly applicable to environment variables, and not all shell variables *are* environment variables, but they do share a namespace so the conventions apply. – Charles Duffy Jun 17 '16 at 20:40
  • 2
    [Aside: Did you know that if a variable is already exported, then all changes go straight to the environment, so you don't need to export it again? So in many contexts `export PATH=$PATH:foo` is silly, because `PATH` is already an environment variable, so `PATH=$PATH:foo` would do the exact same thing]. – Charles Duffy Jun 17 '16 at 20:43

1 Answers1

6

The two lines of code (better to use double brackets):

[[ -r /etc/java/java.conf ]] && . /etc/java/java.conf
export JAVA_HOME

...equates to this:

-r checks if /etc/java/java.conf exists and read permission is granted.

&& if the condition above is true then source the file.

export JAVA_HOME takes the previously-assigned value of $JAVA_HOME from the sourced file making it available to subprocesses rather than only within the shell.

Community
  • 1
  • 1
l'L'l
  • 44,951
  • 10
  • 95
  • 146
  • 1
    It doesn't "take it from the read file" in the `export` command -- that was done earlier, when it was sourced, which assigned a shell variable. This exports the shell variable, making it an environment variable also. These are very distinct operations; an explanation that conflates them doesn't help understanding. – Charles Duffy Jun 17 '16 at 20:35
  • @CharlesDuffy: Thank you! – l'L'l Jun 17 '16 at 20:47
  • 1
    NP -- as edited, I think this is a very clear and helpful answer! – Charles Duffy Jun 17 '16 at 20:48