Changing environment variables
Solution 1
/etc/environment
takes a proper re-login to take effect, because it is processed by PAM at login. Further, as @przemo noted, it is not run or sourced as a script, so variables are not expanded. Put such variables in a .sh
file in /etc/profile.d/
:
sudo tee -a /etc/profile.d/my_vars.sh <<"EOF"
export M2_HOME=/usr/local/apache-maven/apache-maven-3.1.1
export M2=$M2_HOME/bin
export PATH=$M2:$PATH
export JAVA_HOME=/usr/local/jdk1.6.0_45
export PATH=$JAVA_HOME:$PATH
EOF
This will also take a re-login to take full effect, but you can test it out immediately by running a login shell:
$ bash -l
$ echo $PATH
/usr/local/jdk1.6.0_45:/usr/local/apache-maven/apache-maven-3.1.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
As Gunnar has pointed out, the default assignment to PATH
should remain in /etc/environment
, and shouldn't be added to the above script. See this community wiki page for more information.
Solution 2
/etc/environment
is not a script file, you cannot use variables, for further reading I recommend https://help.ubuntu.com/community/EnvironmentVariables
Related videos on Youtube
teaLeef
Updated on September 18, 2022Comments
-
teaLeef over 1 year
I just changed my
/etc/environment
file to:M2_HOME=/usr/local/apache-maven/apache-maven-3.1.1 M2=$M2_HOME/bin PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games" PATH=$M2:$PATH JAVA_HOME=/usr/local/jdk1.6.0_45 PATH=$JAVA_HOME:$PATH
(trying to do something similar to this)
However, when I tried checking the environment variables by typing, for instance
echo "$M2"
, the result is an empty line for all the variables and $PATH is the same as before editing the file. Why didn't my changes operate? I tried closing and opening the shell, but nothing happens. -
Gunnar Hjalmarsson over 9 yearsI would have left the original PATH assignment in
/etc/environment
where it is located by default. Otherwise I agree. -
muru over 9 years@GunnarHjalmarsson I saw the variables being set before PATH and thought OP may have modified it in some way. So I copy-pasted that part. On closer examination, I agree, that looks like the default PATH and I'll edit the answer.