An Environment Variable is a name associated with a value (typically numbers and strings or words), that can affect the way a running process or program behaves on a computer
They are part of the environment in which a process or program runs
Environment Variables can be
HOME
which stores the directory allotted to a specific user by an operating system echo
command can be used to get the value of an environment variable in a Linux Terminal, with $
as the prefix of the variable
echo $HOME
/home/izaku
In windows(Command Prompt, cmd.exe) the variable is enclosed with %
characters
echo %HOME%
/home/izaku
printenv
command can be used to display list of environment variables and their values
To retrieve a specific environment variable, its name is passed with printenv
command
A shell variable DW
can be set to /home/izaku/Downloads
using a simple =
in a Terminal
This shell variable is available to the shell instance, which can be retrieved using echo
command
But printenv
command doesn't return its value, which shows that it is not an environment variable
To set it as an environment variable, export
command can be used with the variable like export DW
To make Environment variables persistent those variables have to be defined in a bash configuration file
In linux, environment variables are typically read from the following files:
/etc/environment
- This file is used to set up system-wide environment variables
Variables in this file are set in the following format :
FOO=bar
VAR_TEST="Test Var"
/etc/profile
- Variables set in this file are loaded whenever a bash login shell is entered
export
is used here to set variables
export JAVA_HOME="/path/to/java/home"
export PATH=$PATH:$JAVA_HOME/bin
It can be ~/.bashrc
or ~/.zshrc
or ~/.bash_profile
pr ~/.zsh_profile
based on shell being used
export
is used here to define variables
export PATH="$HOME/bin:$PATH"
Use source
command to load the new environment variables into the current shell session:
source ~/.bashrc