"Pointers" with $ Syntax in /etc/environment

Is it valid to have "pointers" in /etc/environment using $FOO syntax? Right now my /etc/environment looks like this:

JAVA_HOME=/usr/lib/jvm/java-6-openjdk
MAVEN_HOME=/usr/bin/apache-maven/apache-maven-3.0.4
M2_HOME=$MAVEN_HOME
M2=$MAVEN_HOME/bin
PATH=/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:$JAVA_HOME/bin:$MAVEN_HOME/bin

I'm not getting the results that I'm expecting though:

~$ echo $JAVA_HOME
/usr/lib/jvm/java-6-openjdk
~$ echo $MAVEN_HOME
/usr/bin/apache-maven/apache-maven-3.0.4
~$ echo $PATH
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin/bin:/usr/games:$JAVA_HOME/bin:$MAVEN_HOME/bin

I didn't find much documentation on /etc/environment, but the reason things don't work the way you expect is that it's "not a script file", and thus is not processed by the shell, so referencing a previously-defined variable doesn't work. This explains the behaviour you're seeing.

You're better off defining these kinds of environment variables in your home, in .profile or .bashrc, or if you need them for all users, in /etc/profile.d or /etc/bash.bashrc.

There's some pretty extensive documentation on setting environment variables here, and it points to either .profile or .bashrc as the preferred place to put this stuff.


The canonical source for the information you seek (at least for flavours of Debian that use SystemD; and derivatives thereof, such as modern versions of Ubuntu) is environment.d(5) (some formatting in this excerpt has been changed at my discretion for clarity):

Configuration format

The configuration files contain a list of KEY=VALUE environment variable assignments, separated by newlines. The right-hand side of these assignments may reference previously defined environment variables, using the ${OTHER_KEY} and $OTHER_KEY format. It is also possible to use ${FOO:-DEFAULT_VALUE} to expand in the same way as ${FOO} unless the expansion would be empty, in which case it expands to DEFAULT_VALUE, and use ${FOO:+ALTERNATE_VALUE} to expand to ALTERNATE_VALUE as long as ${FOO} would have expanded to a non-empty value. No other elements of shell syntax are supported.

Each KEY must be a valid variable name. Empty lines and lines beginning with the comment character # are ignored.

Example

Example 1. Setup environment to allow access to a program installed in /opt/foo

/etc/environment.d/60-foo.conf :–

FOO_DEBUG=force-software-gl,log-verbose
PATH=/opt/foo/bin:$PATH
LD_LIBRARY_PATH=/opt/foo/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
XDG_DATA_DIRS=/opt/foo/share:${XDG_DATA_DIRS:-/usr/local/share/:/usr/share/}