What is an environment variable?
An environment variable (env var) is a key-value pair that stores information about the environment in which a process runs.
Env vars can be used to control the behavior of a program if that program expects its configuration to be set via environment variables.
Be aware that env vars are case-sensitive, so the env var MY_VAR is different from the env var
my_var.
What are the most common env vars?
Some of the most common env vars are:
| Variable | Description |
|---|---|
| USER | The name of the logged-in user. |
| PWD | The current directory. |
| HOME | The user's home directory. |
| PATH | List of directories to search for programs: /usr/local/bin, /usr/bin, /bin, etc. |
| SHELL | The current shell used (/bin/bash, /bin/sh, etc). |
How can I list all env vars?
You can run either printenv or env to list all env vars available in the current shell:
printenv
printenv usage
Description: printenv shows the value of an environment variable.
Usage: printenv [ENV VAR]
Example: printenv PATH
This example shows the value of the environment variable PATH.
Note: printenv shows all environment variables defined in the current shell if no variable is provided.
env
env usage
Description: env sets environment variables before running a command.
Usage: env VAR=VALUE PROGRAM
Example: env MY_VAR=my_value myapp
This example sets the environment variable MY_VAR to the value my_value and runs the program myapp.
Common options:
-
-u VAR: delete the environment variableVARbefore running the program.
Note: env shows all environment variables defined in the current shell if no arguments are provided.
How can I show the value of a single env var?
You can use printenv for this:
printenv PATH
How can I set an env var?
If you want to set the env var just to run a single program, but not in the current shell:
env MY_VAR=my_value bash -c 'echo the value of MY_VAR is $MY_VAR'
If you want to set the env var in the current shell (all programs you run from now on will inherit it):
export MY_VAR=my_value
export usage
Description: export sets environment variables in the current shell.
Usage: export VAR=VALUE
Example: export MY_VAR=my_value
This example sets the environment variable MY_VAR to the value my_value in the
current shell.
Common options:
-
-n VAR: delete the environment variableVAR.
bash -c 'echo the value of MY_VAR is $MY_VAR'
What does bash -c 'echo the value of MY_VAR is $MY_VAR' do?
It runs the command echo the value of MY_VAR is $MY_VAR in a new shell. Only env vars are inherited
by programs run from the current shell, so if the value is empty, it means that the env var was not set.
Can I use MY_NEW_VAR=my_new_value to define an env var without using export?
No, you can't. That would define a shell variable, not an environment variable. That means that the variable
MY_NEW_VAR will not be inherited by the programs you run from this shell.
MY_NEW_VAR=my_new_value
printenv MY_NEW_VAR
Nothing is returned from the last command because MY_NEW_VAR is a shell variable, not an environment
variable.
Are env vars defined with export permanent?
No, env vars are temporary. They are lost when the current shell is closed.
Is there any way to make an env var permanent across shell sessions?
Yes, you can persist env vars across shell sessions by adding them to some configuration files like
~/.profile or /etc/profile.
How can I modify an env var?
You can modify an env var by setting it again with a new value:
export MY_VAR=my_new_value
How can I delete an env var?
If you want to delete an env var just for the program you want to run:
env -u MY_VAR bash -c 'echo the value of MY_VAR is $MY_VAR'
If you want to delete an env var in the current shell (all programs you run from now on will not inherit it), you
can use either unset or export -n:
unset MY_VAR
unset usage
Description: unset deletes an environment variable in the current shell.
Usage: unset VAR
Example: unset MY_VAR
This example deletes the environment variable MY_VAR in the current shell.
export -n MY_VAR
Where can I find examples of env vars used in real-life situations?
- The twelve-factor app (config section)
- Kubernetes: define environment variables for a container
- Docker: set environment variables
Where can I find more info about env vars?
Man pages:
man printenv
man env
External link: Wikipedia