1

I have set my environmental variable $JAVA_HOME within the /etc/environment file. Worked like a charm! Now I want to remove it. After removing the line completely from the environment file, then do a reboot, it continues to resolve the path on:

echo $JAVA_HOME

How do I clean that out?

1
  • Although I posted an answer on how to unset a variable, it appears you might be setting $JAVA_HOME in more than one place. What does it read when you type echo $JAVA_HOME? Apr 6, 2019 at 1:36

1 Answer 1

4

$JAVA_HOME will already have a definition on many systems without explicitly declaring it in /etc/environment:

$ echo $JAVA_HOME
/usr/lib/jvm/java-8-oracle

That said, the unset command will eliminate a variable. From: Unix / Linux - Using Shell Variables

Unsetting Variables

Unsetting or deleting a variable directs the shell to remove the variable from the list of variables that it tracks. Once you unset a variable, you cannot access the stored value in the variable.

Following is the syntax to unset a defined variable using the unset command −

unset variable_name

The above command unsets the value of a defined variable. Here is a simple example that demonstrates how the command works −

#!/bin/sh

NAME="Zara Ali"
unset NAME
echo $NAME

In our case we can use:

$ echo $JAVA_HOME
/usr/lib/jvm/java-8-oracle
───────────────────────────────────────────────────────────────────────────────────────────
$ unset JAVA_HOME
───────────────────────────────────────────────────────────────────────────────────────────
$ echo $JAVA_HOME

───────────────────────────────────────────────────────────────────────────────────────────
$ 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .