7

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?

sourav c.
  • 46,120

2 Answers2

6

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.

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.

sourav c.
  • 46,120
Jos
  • 30,529
  • 8
  • 89
  • 96