Environment variables

Environment variables in Linux

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 variable VAR before 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 variable VAR.
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

Environment variables in Docker

How can I set an env var inside a Docker container?

You can use the --env (or -e) flag when running the container:

docker run --env MY_VAR=my_value alpine printenv MY_VAR

This command will:

  1. Set the environment variable MY_VAR to the value my_value before running an Alpine container.
  2. Print the value of the env var MY_VAR inside that container.
Can I pass env vars already defined in my shell to a Docker container?

Yes, if you don't set a value when using the --env flag, Docker will read the value of the env var from your shell if it's defined:

export MY_VAR=new_value
docker run --env MY_VAR alpine printenv MY_VAR

The second command will set the env var MY_VAR to the value defined in the first command before running the container.

Can I set multiple env vars inside a Docker container?

Yes, you can use the --env flag multiple times:

docker run --env MY_VAR1=my_value1 --env MY_VAR2=my_value2 alpine printenv

This command will set the env vars MY_VAR1 and MY_VAR2 before running the container and print all the env vars defined inside that container.

Is there a more convenient way if I want to set many env vars inside a Docker container?

Yes, you can use the --env-file flag to pass a file containing multiple env vars to a Docker container:

echo -e 'MY_VAR1=my_value1\nMY_VAR2=my_value2' > my-env-vars.txt

This command will create a file called my-env-vars.txt with 2 env vars defined: MY_VAR1=my_value1 and MY_VAR2=my_value2.

docker run --env-file my-env-vars.txt alpine printenv

This command will set the env vars defined in the file my-env-vars.txt before running a container and print all the env vars defined inside that container.

Can I set default values for env vars in a Docker image?

Yes, you can set default values for env vars using the ENV instruction in a Dockerfile:

Real-world scenarios

Where can I practice using environment variables in a real-world scenario?

You can apply what you've just learned in these free interactive scenarios:

More info

Where can I find more info about env vars?

Man pages:

man printenv
man env

External links: