Learn how to manage Unix environment variables.
This material is part of architecture & deployment course for Media Engineering.
You will need
Recommended reading
Unix Environment Variables > Environment
In the context of software deployment, an environment is a computer system or set of systems in which a program or component is deployed and executed.
When you develop and/or execute a program locally, your local computer is an environment.
When you transfer a program to a server and execute it there, that becomes another environment.
Unix Environment Variables > Environment
In industrial use, the development environment (where changes are originally made) and production environment (what end users use) are separated, often with several stages in between.
The configuration of each environment may vary to suit the requirements of development, testing, production, etc.
Unix Environment Variables > Environment
When using agile software development, teams are seeing much higher quantities of software releases.
Continuous delivery and DevOps are processes where a program is packaged and "moved" from one environment to the other (i.e. deployed) until it reaches the production stage.
Modern software development teams use automation to speed up this process.
Unix Environment Variables > Environment variables
An environment variable is a named value that can affect the way running processes will behave on a computer.
When a process runs on a Unix system, it may query variables such as:
HOME
- The home directory of the user running the process.LANG
- The default locale.TMP
- The directory in which to store temporary files.Another common example is the PATH
,
an environment variable that indicates in which directories to look for binaries to execute when typing commands in a shell.
Unix Environment Variables > Environment variables
Environment variables can affect the behavior of programs without modifying them.
If a program bases some of its behavior on an environment variable, you can simply change the value of the variable before running it, allowing you to customize it without changing one line of code.
Environment variables can be used as a dynamic means of configuration, an alternative to configuration files or hardcoded values.
Getting them, listing them, setting them, deleting them.
Unix Environment Variables > Managing environment variables
To display the current value of an environment variable, use the echo
command.
A variable can be referenced by its name prefixed with a dollar sign ($
):
$> echo $USERubuntu$> echo $HOME/home/ubuntu$> echo $SHELL/bin/bash$> echo $PATH/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
If a variable is not set, nothing will be displayed:
$> echo $FOO
Unix Environment Variables > Managing environment variables
The env
command prints all environment variables currently set in your shell, and their values:
$> envLC_ALL=en_US.utf-8LS_COLORS=rs=0:di=01;34:...LANG=C.UTF-8USER=ubuntuPWD=/home/ubuntuHOME=/home/ubuntuLC_CTYPE=UTF-8SSH_TTY=/dev/pts/0MAIL=/var/mail/ubuntuTERM=xterm-256colorSHELL=/bin/bashPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Unix Environment Variables > Managing environment variables
There are multiple ways to set an environment variable. The lifetime of the variable depends on how you set it:
.bashrc
).In order to test these techniques, download this simple script which prints the value of an environment variable if set:
$> curl -sL https://git.io/fpdar > print-env-var.sh$> chmod 755 print-env-var.sh$> ./print-env-var.sh PATHThe value of $PATH is /usr/local/sbin:/usr/local/bin:...$> ./print-env-var.sh FOO$FOO is not set
Unix Environment Variables > Managing environment variables > Setting an environment variable
You can prefix a command by an environment variable assigment:
$> FOO=bar ./print-env-var.sh FOOThe value of $FOO is bar
This only sets the variable for the process executed by that command. As you can see, the variable is still not set if we check later, even in the same shell session:
$> ./print-env-var.sh FOO$FOO is not set
Unix Environment Variables > Managing environment variables > Setting an environment variable
The export
command exports an environment variable to all the child processes running in the current shell session:
$> export FOO=bar$> ./print-env-var.sh FOOThe value of $FOO is bar$> ./print-env-var.sh FOOThe value of $FOO is bar
As you can see, the variable remains set.
However, if you close the shell and reopen a new one, the variable is no longer set:
$> ./print-env-var.sh FOO$FOO is not set
Unix Environment Variables > Managing environment variables > Setting an environment variable
If you add the export
command to your shell configuration file (.bash_profile
for Bash),
it will be run every time you start a new shell:
$> echo 'export FOO=bar' >> ~/.bash_profile$> cat ~/.bash_profileexport FOO=bar
This will not immediately take effect in the current shell,
as the configuration file is only evaluated when the shell starts.
But you can evaluate it with the source
command:
$> source ~/.bash_profile$> ./print-env-var.sh FOOThe value of $FOO is bar
The variable will still be set if you close this shell and launch another one:
$> ./print-env-var.sh FOOThe value of $FOO is bar
Unix Environment Variables > Managing environment variables
The unset
command removes a variable from the environment:
$> ./print-env-var.sh FOOThe value of $FOO is bar$> unset FOO$> ./print-env-var.sh FOO$FOO is not set
Of course, if the variable is exported in your shell configuration file, this will only remove it for the current shell session. You must remove the export from the configuration file to remove the variable from future shells.
Unix Environment Variables > Managing environment variables
Every programming language has a simple way of retrieving the value of environment variables:
Language | Code |
---|---|
C | getenv("PATH") |
Elixir | System.get_env("FOO") |
Erlang | os.get_env("FOO") |
Go | os.Getenv("FOO") |
Java | System.getenv("FOO") |
Node.js | process.env.FOO |
PHP | getenv("FOO") |
Python | os.getenv("FOO") |
Ruby | ENV["FOO"] |
Rust | env::var("FOO") |
Unix Environment Variables > Managing environment variables
You may put whatever kind of value you want into an environment variable:
export MEANING_OF_LIFE=42 # A numberexport PERSON='{"name":"John Doe","age":24}' # Serialized JSON
In your programming language of choice, however, the value will always be a character string. It's up to you to parse it if you want to use it as another type, for example in Node.js:
> console.log(process.env.MEANING_OF_LIFE);42> process.env.MEANING_OF_LIFE + 2'422'> typeof process.env.MEANING_OF_LIFEstring> parseInt(process.env.MEANING_OF_LIFE, 10) + 244> process.env.PERSON.nameundefined> JSON.parse(process.env.PERSON).name'John Doe'
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |