Newby Coder header banner

Environment Variable

What is an Environment Variable

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

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

Display environment variables

printenv command can be used to display list of environment variables and their values

cl-linux-cmd-printenv

To retrieve a specific environment variable, its name is passed with printenv command

cl-linux-cmd-printenv-var

Set Environment variables

Setting environment variables temporarily

A shell variable DW can be set to /home/izaku/Downloads using a simple = in a Terminal

cl-linux-env-var-set

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

cl-linux-env-var-export

Setting Persistent environment variables

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: