2

The environment is Ubuntu 18.

If I add one line JAVA_HOME="/usr/java11" in /etc/environment, and execute source /etc/environment , I can echo this environment variable:

echo $JAVA_HOME
/usr/java11

But if I try to get it from export, this variable is not in the list:

export | grep JAVA_HOME
--result is blank--

Then I use export $JAVA_HOME=/usr/java8 to export a variable (notice here is java8, not java 11), I can have:

export |grep JAVA_HOME
declare -x JAVA_HOME="/usr/java8"

Now, I can still echo $JAVA_HOME as /usr/java11:

echo $JAVA_HOME
/usr/java11

Question: what is the difference of echo $variable and export | grep JAVA_HOME?

I try a simple python program, os.environ.get("JAVA_HOME") returns "/usr/java8" from the export, not the echo.

6
  • 1
    You mention Ubuntu 18, so is this a contained Ubuntu Core 18 or snap only environment? as the confinement models in play in 18 releases differ to the more open 18.04 models - please be specific.
    – guiverc
    Sep 22 at 21:34
  • 1
    I think the issue here is that /etc/environment is just a list of name=value pairs. When it is read by pam_env those variables are exported to the environment, but sourcing it into your current shell only creates ordinary shell variables - check with declare -p JAVA_HOME for example. Sep 22 at 21:40
  • @steeldriver , what is "ordinary shell variables", and what are other variables?
    – Ben L
    Sep 22 at 21:47
  • @guiverc sorry I don't know what is 'contained' and what is 'snap'. It is a docker container created from 'FROM ubuntu:18.04'
    – Ben L
    Sep 22 at 21:50
  • 1
    Ubuntu 18.04 is a different product to the 18 products; Ubuntu normal deb based releases (without containment) use the year.month format; differing from the 18 or year based products that use containment. Ubuntu has had year based products since 2016 - you should be precise as 18 & 18.04 represent different products (a 18 release cannot use a deb package - it's snap only) fyi: confinement models can vary on snap; but if you're familiar with containers you ~know this; it's the ~same with different terminology..
    – guiverc
    Sep 22 at 22:02
12

The issue here is not really the difference between echo and export, but rather the difference between an environment variable and a simple shell variable (and also about how the /etc/environment file is normally used).

In particular, although /etc/environment happens to contain lines of the form name=value that are valid as POSIX shell variable assignments, its primary purpose (in a modern Linux system) is to be read by the pam_env module during initialization of a user's session - it is pam_env that exports them to the user's environment.

When you source /etc/environment into your shell, there's no special magic that tells the shell that the assignments are those of environment variables (which are exported to the environment, and hence inherited by sub-processes) rather than regular shell variables (which are just available in the current shell scope).

Next time you log in, pam_env will do its magic and JAVA_HOME will then appear in the output of export | grep JAVA_HOME.

See also

2
  • Thanks to point out the difference between "shell variable" and "environment variable".
    – Ben L
    Sep 22 at 22:51
  • 1
    "environmental variables" technically also are "shell variables". They are a subset.
    – vanadium
    Sep 23 at 12:49
4

echo and export are very different commands in the first place.

  • echo will display text. In echo $JAVA_HOME, the shell will substitute $JAVA_HOME with the contents of the shell variable JAVA_HOME it it is defined. Otherwise, $JAVA_HOME will return an empty string.
  • export provides the "export" attribute to the shell variable. export JAVA_HOME will set the export attribute, i.e., the variable will also be available in the environment of any sub shell or sub process rather than in your current shell only. If the variable is not yet set, you can define it while exporting as in export JAVA_HOME=/usr/java11.

In /etc/environment, environment variables are registered with the syntax of a variable assignment. The content of /etc/environment in a default Ubuntu install indeed can be executed. So if you execute the line you included:

`JAVA_HOME=/usr/java11`

Then, all that will do is assign the shell variable PATH the current value.

However, because you included the variable in /etc/environment, it should be effectively be exported during the next startup of your system. Then, it should exist in export and show up in echo $JAVA_HOME in the first terminal you open. So what you currently observe is because you did not yet restart the machine after modifying /etc/environment (and did not export the variable in other ways).

3

Building on the other answers here, some commands that parallel export but for other categories of variables are set (which works for e.g. VARIABLE=value then set | grep VARIABLE) and env

Each of these three commands, when given no arguments, prints a list of variables; which variables they will print has to do with the kinds of variables the command manages.

See What's the difference between set, export and env and when should I use each?

echo $VARIABLE always works since all kinds of variables can be read as $VARIABLE; however, this means it gives you no information on what kind of variable it is or where it comes from.

0

echo is a command for printing text and variables to stdout (or redirect).

export lists the current exported variables in the shell.

This thread explains why you would use export much better than I can:

https://stackoverflow.com/questions/7411455/what-does-export-do-in-shell-programming ... it does a great job explaining what export is fo

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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