02. Acclimating to our Environment printenv, set

Our environment is the area that the shell builds every time a new session is started. Programs are able to access our environment variables to their tailor functions according to a user's settings.

For example, a program can access where your TEMP folder is through the environment variable TMPDIR.

There are two types of variables stored - environment and shell. Let's discuss the environment variables first.

Environment Variables

Environment variables are the variables that define the current shell session, and any child shells or processes that spawn from it.

We can see the environment variables with the printenv command.

$ printenv 
TERM_PROGRAM=iTerm.app
SHELL=/bin/bash
TERM=xterm-256color
USER=JohnDoe
MAIL=/var/mail/JohnDoe
PATH=/usr/local/bin:/usr/bin:/bin
PWD=/home/JohnDoe
LANG=en_US.UTF-8
SHLVL=1
HOME=/home/JohnDoe
LOGNAME=JohnDoe
LESSOPEN=| /usr/bin/lesspipe %s
LESSCLOSE=/usr/bin/lesspipe %s %s
_=/usr/bin/printenv

You'll find quite a few variables. You can also list just one variable by typing the variable name as an argument to printenv.

$ printenv USER
JohnDoe

Here's just some of the most common environment variables.

DISPLAY
Name of your current display.
EDITOR
Name of program to be used for text editing.
SHELL
Name of shell program (bash by default).
HOME
Path to user's home directory.
LANG
The character set and collation order of your language.
MAIL
Where a user's mail is found.
OLD_PWD
Previous working directory (same as cd -).
PAGER
Name of the program to be used for paging output.
PATH
Colon-separated list of directories where the shell searches before executing a command.
PS1
Prompt string 1, or how your shell prompt should appear.
In our case, we have a simple $
PWD
Current working directory path.
TEMP
Where a user can store temporary files.
TERM
Type of computer terminal being used.
TERM_PROGRAM
Type of program used as a terminal.
TZ
Time zone.
USER
Current logged in user.
_
Most recently executed command.

env - printenv's sibling command

In addition to the printenv command is env. These two commands are very similar, except printenv can request values of individual variables, while env lets you pass variables through to a command.

The env variable is useful when you want to create child processes with an alternate variable. The -c option here allows you to pass in a command as a string.

$ env VAR="HelloWorld" bash -c 'command'

Shell Variables

Shell variables are useful for keeping values in scripts, and control the way the shell behaves. To see the shell variables use the set command. This displays the shell variables, environmental variables, local variables and shell functions.

$ set
...
DIRSTACK=()
EUID=501
GROUPS=()
HISTFILE=/Users/shawnPC/.bash_history
HISTFILESIZE=500
HISTSIZE=500
HOME=/Users/shawnPC
HOSTNAME=Shawns-Mac-Pro
HOSTTYPE=x86_64
IFS=$' \t\n'
ITERM_PROFILE=Default
ITERM_SESSION_ID=w0t0p0
...

Since this will have many more variables, consider piping the listing to the less command.

Here are some common shell variables:

BASHOPTS
List of options when bash was executed.
BASH_VERSION
Version of the bash in use.
COLUMNS
Number of columns that are being used to draw output on screen.
DIRSTACK
Stack of directories available with pushd an popd commands.
HISTSIZE
Number of lines of command history allowed in memory.
PS2
How the secondary prompt should appear (commands spanning multiple lines).
UID
User id of current user.

Aching back from coding all day?

Self-Massage Tool

Aching back from coding all day? Try Back Problems

Relieve spasms, tight muscles, trigger points and pressure points with the Body Back Buddy! This trigger point massage is designed to help you self-message any area of your body - especially those that are hard to reach. Keeping your muscles relaxes and out of contraction is importan in helping to reduce pain and prevent muscle injury.

$ Check price
29.9529.95Amazon 4.5 logo(3,443+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

The Linux Command Line

Take your Linux skills to the next level! Try Linux & UNIX

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources

Ad