Environment Variables
2020-11-08
2 minutes
Basically environment variables are stored in the system and can be used by different shell processes. Environment variables are usually upper case (conventionally) and and follow Bash syntax rules. Environment variables will be available to any shell process. You can set an environment variable in a one line command using export
(which sets the environment variable) and we can print the value of the environment variable with printenv
.
export KEY=value
printenv KEY
$ value
To remove the environment variable we can use unset
(to unset shell/environment variables).
unset KEY
The other option is to use set
(to set shell variables) and export
together.
set KEY=value
export KEY
printenv KEY
If you set the environment variable in one shell using the command line and then move to a new terminal to print the value then nothing will return. In order for the environment variables to be recognized in new shell processes you need to set them in your bash profile. For example, in ~/.bash_profile
you could have the following.
export KEY=value
If you save the file and open up a new shell process and run printenv KEY
from the command line then value
will be returned.
A common use case of environment variables is with the PATH
variable. Some programs will search this variable for executable. For example, one of the paths in my PATH
variable points to the location of texbin so that LaTeX knows where to look when I try to build LaTeX code.
Reference:
247 Words