Let's take the famous environment variable: PATH. This variable tells the system where to look for executable binaries in the file system. For example, Ubuntu has the following PATH variable definition by default:
/home/linuxedo/.local/bin:/home/linuxedo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
:
).
This PATH variable tells Ubuntu to look for executable binaries in any of the
folders included in the variable definition.printenv
will print all the environment variables along with their
values in the terminal.echo
command as shown below.echo $PATH
Setting Environment Variables
There are three different use cases with environment variable settings.- Temporary environment variables
- Environment variables for the current user
- Environment variables for all users
1. Temporary Variables
Temporary environment variables are used to change the behavior of a program or feed user input to a program by changing the environment variable in the current session.
Step 1:
To make those changes, use the export
command as shown below.
export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_301/
export PATH=$PATH:/usr/lib/jvm/jdk1.8.0_301/bin
Step 2:
After setting the variable, use the echo command to check if it has been correctly
applied.
echo $JAVA_HOME
echo $PATH
Step 3 [Optional]:
To remove a variable, use the unset
command as shown
below. It will also get lost if you close the terminal and open a new one.
unset JAVA_HOME
2. Environment Variables for the Current User
To set an environment
variable for the current user, add the export command to the ~/.bash_profile
file. If the ~/.bash_profile file does not already exist, simply create a new file.
Step 1:
Open the ~/.bash_profile file in your favorite editor.
nano ~/.bash_profile
Step 2:
Append the export command to the end of the
file.
export PATH=$PATH:/usr/lib/jvm/jdk1.8.0_301/bin
Step 3:
Save
the changes and close the editor
Ctrl+O and Ctrl+X
Step 4:
After
changing the ~/.bash_profile file, you need to restart the computer to apply the
changes. However, the following command will load the changes into the terminal
executing this command.
source ~/.bash_profile
echo $PATH
3. Environment Variables for All Users
Step 1:
To
apply the environment variable for all users, open the /etc/environment variable
as root user in your favorite editor.
sudo nano /etc/environment
If the file already has PATH variable, append your new path to that variable by separating the directories using a colon.
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/jdk1.8.0_301/bin"
JAVA_HOME="/usr/lib/jvm/jdk1.8.0_301"
In some distributions, the /etc/environment file may be empty. In such scenarios, use the following PATH variable definition to avoid breaking the system.
PATH="$PATH:/usr/lib/jvm/jdk1.8.0_301/bin"
JAVA_HOME="/usr/lib/jvm/jdk1.8.0_301"
Corrupting the system-level PATH variable may break your system and prevent you from logging into the system next time.
Step 3:
After making
all the changes, save and close the editor.
Ctrl+O and Ctrl+X
Step 4:
Similar to the
~/.bash_profile, you need to restart the system to apply these changes. However,
you can use the source command to load the changes into the active terminal as
shown below.
source /etc/environment
Check if the changes have been applied successfully.
echo $PATH
echo $JAVA_HOME
No comments
Post a Comment