Referencing environment variables *in* /etc/environment?
Solution 1:
The /etc/environment
file is read by the pam_env
PAM module. It only supports simple key-value pairs, with no substitution on the right-hand side.
If you want to build the value with substitutions or other shell expansions, you need to go through a shell. /etc/profile
(or a file in /etc/profile.d
) is the right place for these. This is the traditional place (/etc/environment
is a relatively recent innovation, in the history of unix systems), and there's certainly nothing sloppy about using it. The main benefit of /etc/environment
is that it allows environment variables to be set even with login methods that do not involve a shell or for users who use a non-Bourne shell, but these are not common in practice.
Solution 2:
The Ubuntu documentation makes this seem impossible. BUT, there is an answer.
ANSWER: Create persistent, system wide environment variables in /etc/.bashrc
JAVA_HOME="/tools/java"
...
PATH="$JAVA_HOME:$PATH"
Details:
Here are some more details from the Environment Variables documentation.
The official recommendation is to use /etc/environment
, as you have tried. The problem is this file is not a script file. In other words, you cannot use variables in variable declarations.
The documentation continues with 2 other "Not Recommended" files/etc/profile
and /etc/bash.bashrc
.
/etc/profile
"does little more than invoke the /etc/bash.bashrc file". So instead choose
/etc/bash.bashrc
because "Ubuntu is configured by default to execute this file whenever a user enters a shell or the desktop environment."