Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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
share|improve this question

3 Answers 3

up vote 2 down vote accepted

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.

share|improve this answer
1  
Thanks for the reply. From the post that you cited though: "Environment variable settings that affect the system as a whole (rather then just a particular user) should not be placed in any of the many system-level scripts that get executed when the system or the desktop session are loaded, but into /etc/environment - This file is specifically meant for system-wide environment variable settings. It is not a script file, but rather consists of assignment expressions, one per line. Specifically, this file stores the system-wide locale and path settings." Is that guidance incorrect? –  Tyson Trautmann Jun 12 '12 at 16:12
    
I'd say it's correct, however you'd lose the ability to reference a variable inside a definition, so you'd have to spell out the full JAVA_HOME and MAVEN_HOME in the PATH variable definition. If you want that, and you also want this to be available for all users, I'd say /etc/profile.d would be the place to put it, as it's designed so you can drop a separate file with your variables, thus you're not even modifying existing config files, so your changes should survive through upgrades. –  roadmr Jun 12 '12 at 18:56
    
Thanks, will either update the paths to the full/literal paths or will use one of those alternate files. Appreciate the help! –  Tyson Trautmann Jun 13 '12 at 3:11

You can use pointers in /etc/environment but have to use a different syntax:

try:

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"

Use the curly braces {} around the variable names when referring to another variable assignment. So, instead of $JAVA_HOME, use ${JAVA_HOME}. It also seems to be necessary to have quotation marks around the value of a variable in which you have pointer to other variables:

so PATH="... ${SOME_VAR} ..."

share|improve this answer
    
I tried this, and it didn't seem to work for me. I get the literal ${FOO} in the path. –  Tyson Trautmann May 15 '13 at 23:04

The Ubuntu documentation makes this seem impossible. BUT, there is an answer.

ANSWER: Create persistent, system wide environment variables in /etc/bash.bashrc

JAVA_HOME="/tools/java"
...
PATH="$JAVA_HOME:$PATH"  

Details:
I posted a lengthier answer on this AskUbuntu question

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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