Saturday, May 18, 2024
13
rated 0 times [  13] [ 0]  / answers: 1 / hits: 5600  / 3 Years ago, fri, october 15, 2021, 9:18:41

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.


More From » command-line

 Answers
3

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



[#1163] Saturday, October 16, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
pilun

Total Points: 270
Total Questions: 100
Total Answers: 94

Location: England
Member since Sat, Feb 13, 2021
3 Years ago
pilun questions
;