+ - 0:00:00
Notes for current slide
Notes for next slide

Unix Environment Variables

Learn how to manage Unix environment variables.

This material is part of architecture & deployment course for Media Engineering.

You will need

  • A Unix CLI

Recommended reading

1 / 19

Environment

Unix Environment Variables

2 / 19

What is an environment?

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.

3 / 19

Deployment

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.

4 / 19

Release management

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.

5 / 19

Environment variables

Unix Environment Variables

Affecting processes since 1979.

6 / 19

What is an environment variable?

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.
  • And more.

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.

7 / 19

What are they for?

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.

8 / 19

Managing environment variables

Unix Environment Variables

Getting them, listing them, setting them, deleting them.

9 / 19

Getting an environment variable

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 $USER
ubuntu
$> 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
10 / 19

Listing all environment variables

Unix Environment Variables > Managing environment variables

The env command prints all environment variables currently set in your shell, and their values:

$> env
LC_ALL=en_US.utf-8
LS_COLORS=rs=0:di=01;34:...
LANG=C.UTF-8
USER=ubuntu
PWD=/home/ubuntu
HOME=/home/ubuntu
LC_CTYPE=UTF-8
SSH_TTY=/dev/pts/0
MAIL=/var/mail/ubuntu
TERM=xterm-256color
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
11 / 19

Setting an environment variable

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:

  • You can set it for one command.
  • You can set it for the current shell session.
  • You can set it in your shell configuration file (e.g. .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 PATH
The value of $PATH is /usr/local/sbin:/usr/local/bin:...
$> ./print-env-var.sh FOO
$FOO is not set
12 / 19

Setting a variable for one command

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 FOO
The 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
13 / 19

Setting a variable for a shell session

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 FOO
The value of $FOO is bar
$> ./print-env-var.sh FOO
The 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
14 / 19

Setting a variable in the shell configuration file

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_profile
export 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 FOO
The value of $FOO is bar

The variable will still be set if you close this shell and launch another one:

$> ./print-env-var.sh FOO
The value of $FOO is bar
15 / 19

Removing an environment variable

Unix Environment Variables > Managing environment variables

The unset command removes a variable from the environment:

$> ./print-env-var.sh FOO
The 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.

16 / 19

Getting environment variables from code

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")
17 / 19

Environment variables are always strings

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 number
export 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_LIFE
string
> parseInt(process.env.MEANING_OF_LIFE, 10) + 2
44
> process.env.PERSON.name
undefined
> JSON.parse(process.env.PERSON).name
'John Doe'
18 / 19

Environment

Unix Environment Variables

2 / 19
Paused

Help

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