Skip to content

Environment Variables#

You've already seen a environment variable really, really recently. In the last section's challenge, you played a letting guessing game. The script that you copied used an environment variable - several in fact... $RANDOM.

Environment variables are like little pieces of data stored in and around the environment you're using (hence the name.) They're sort of like having pieces of paper inside your pocket with information on - phone numbers, secret tokens, filenames, etc. - that move around with you as you go about your business, walking about.

But the moment you log out of that system and into another one (or change the garment you're wearing so the pockets change) the environment variables from the previous environment are gone and new ones come into your environment (pocket.)

Log into your VM and on at the shell prompt, type: env. Here's what I get (some things are redacted because they're too long):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
SHELL=/bin/bash
PWD=/home/michael
LOGNAME=michael
XDG_SESSION_TYPE=tty
MOTD_SHOWN=pam
HOME=/home/michael
LANG=en_US.UTF-8
SSH_CONNECTION=10.0.2.2 59411 10.0.2.15 22
LESSCLOSE=/usr/bin/lesspipe %s %s
XDG_SESSION_CLASS=user
TERM=xterm-256color
LESSOPEN=| /usr/bin/lesspipe %s
USER=michael
SHLVL=1
XDG_SESSION_ID=11
XDG_RUNTIME_DIR=/run/user/1000
SSH_CLIENT=10.0.2.2 59411 22
XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktop
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
SSH_TTY=/dev/pts/0
_=/usr/bin/env
OLDPWD=/home/michael/number_game

Notice how there's a pattern: SOME_KEY=some_value? Environment variables consist of two components: a name, which is usually upper case; and a value, which can be just about anything.

Key Points#