3

I tried adding a path to my PATH variable by making changes in /etc/environments. However, now when I do echo $PATH, the PATH variable contains even lesser path entries than it had earlier. I did reboot my computer too.

It must now be picking the value of PATH variable from some other file or the value being read from etc/environment is now being overridden by some other file.

I am unable to fix this. Please help.

Thanks.

Dan
  • 33
  • 1
    What was you trying to add? – Anwar Aug 28 '16 at 11:24
  • Was setting a JAVA_HOME variable. Adding it to the end of existing PATH. Then creating another variable SBT_HOME and adding to the end of PATH variable. – Dan Aug 28 '16 at 11:27
  • what exactly did you put in /etc/environment? – Zanna Aug 28 '16 at 11:28
  • PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/scala/scala-2.11.8/bin:/usr/local/spark/spark-2.0.0-bin-hadoop2.7/bin" JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64 export JAVA_HOME export PATH=$PATH:$JAVA_HOME/bin SBT_HOME=/usr/local/sbt PATH=$PATH:$SBT_HOME/bin export PATH – Dan Aug 28 '16 at 11:29
  • @Dan see the answer below. /etc/environment doesn't work like that. YOu'll add one value per line – Anwar Aug 28 '16 at 11:30
  • see this question too there's no $ expansion in /etc/environment as @Anwar explains – Zanna Aug 28 '16 at 11:31
  • @Anwar - So if I have to write complete path directly using ':' as separator? If I do not use $ and write just PATH=PATH:JAVA_HOME it wont consider these as variables but plain text? – Dan Aug 28 '16 at 11:37
  • Yes @Dan. That's is what Wiki says – Anwar Aug 28 '16 at 11:41
  • @Anwar: Please note that the wiki page takes up an alternative to editing /etc/environment. When it comes to PATH it's probably better to leave the default /etc/environment as is, and modify PATH via a /etc/profile.d/*.sh file. – Gunnar Hjalmarsson Aug 28 '16 at 11:52

2 Answers2

2

I'd suggest that you remove your adjustments from /etc/environment, create the file /etc/profile.d/my-path-additions.sh, and give it this contents:

PATH="$PATH:/u‌​sr/local/scala/scala‌​-2.11.8/bin:/usr/loc‌​al/spark/spark-2.0.0‌​-bin-hadoop2.7/bin"
export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
export SBT_HOME=/usr/local/sbt
PATH="$PATH:$JAVA_HOME/bin:$SBT_HOME/bin"
Gunnar Hjalmarsson
  • 33,540
  • 3
  • 64
  • 94
0

From Ubuntu wiki

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.

So, make sure you're not using scripting there. Only key/value works and should be one assignment per line like this

FOO=bar

So, if you want to add JAVA_HOME, it will be like

JAVA_HOME="/usr/lib/jvm/jdk-1.8"

Without using any $ symbol. Also the variable already defined will be treated as plain text in the next line.

If you want to add multiple entry, you'll separate those using : separator.

Here is an example (the default line) already found on that file

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
Anwar
  • 76,649
  • So I cannot use variables like JAVA_HOME and SBT_HOME to contain the specific path and then use them to be appended at the end of PATH. I wanted to do this, because it looks cleaner and I can make changes easily if required later.

    Can I use /etc/profile or /etc/bash.bashrc for this? What would be the ideal thing to do?

    – Dan Aug 28 '16 at 11:44
  • No. They will be treated as plain text – Anwar Aug 28 '16 at 11:45
  • Also, can you please tell me, what is the order in which these files are read(profile, environment, bash.bashrc)? Which one will take precedence over the other? – Dan Aug 28 '16 at 11:46
  • @Dan: Please see the answer I posted. /etc/profile.d/*.sh files will be sourced after /etc/environment is read, and hence override it. – Gunnar Hjalmarsson Aug 28 '16 at 11:48
  • downvote doesn't work. Opened new terminal and have empty /etc/profile.d folder. cat /etc/environment PATH="nowork" echo $PATH | grep nowork prints nothing – Philip Rego Jun 19 '22 at 16:51