6

Let's say I have an environment variable named JAVA_HOME, defined in /etc/environment file. I need to append a new value JAVA_HOME/bin in the PATH variable. Consider the following,

JAVA_HOME=/usr/apps/jdk1.8.10_1
PATH=/bin/something/:/bin/bash/something:/usr/apps/jdk1.8.10_1/bin

Now if you look at it, if I could replace /usr/apps/jdk1.8.10_1/bin, with something like below, it would be more convenient.

PATH=/bin/something/:/bin/bash/something:JAVA_HOME/bin

How could I do that? Is it %JAVA_HOME%/bin?

2
4

You can't do that in /etc/environment. It's not a script file, and variable expansion does not work there.

To modify PATH system wide, a file with a .sh extension in the /etc/profile.d folder is a better method. It can be named myvars.sh or just about anything, as long as it has the .sh extension. In your case it might look something like this:

export JAVA_HOME=/usr/apps/jdk1.8.10_1
PATH="$PATH:$JAVA_HOME/bin"

That way you keep the default PATH definition in /etc/environment, and modify it in your own file.

Please see the EnvironmentVariables page for reference.

1
  • Thank you @Gunnar Hjalmarsson.. The exact answer I was looking for. I was thinking it is possible to refer to existing variables in /etc/environment. – Romeo Sierra Sep 16 '16 at 13:44
2

Referring to a variable is done by adding a $ in bash. Check this entering:

$ echo $JAVA_HOME
/usr/apps/jdk1.8.10_1

The command interpreter replaces the $variable by its value.

Your command would be:

PATH=/bin/something/:/bin/bash/something:$JAVA_HOME/bin

Don't use spaces, or your command won't work.

Note: The %symbols% around a variable name are Microsoft's style.

2
  • I downvoted your answer, since the OP asked about /etc/environment, which is not a script file. Please see my answer. – Gunnar Hjalmarsson Sep 16 '16 at 13:09
  • 1
    Thank you for the points mentioned @Jos. I find them really helpful in general.. – Romeo Sierra Sep 16 '16 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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