What are Linux Environment Variables? | Linux
Tutorial for Beginners (4:45)
Code Examples and Video Script
Welcome. Today's question: What is the Linux Environment?
I'm Paul, and as we transition from visual interfaces, like Windows and
Mac, to Linux and the command line, we need to know how to access
what's going on behind the scenes.
Step 1 - The Linux Environment
So here, we'll explore the environment, which is basically a place
where commands find your custom settings.
We'll discuss the two parts of the environment and how to view and add
Then, we'll practice changing variables before heading to our final
video in Project 2, setting the configuration for our easiest-to-learn
text editor nano.
(We will work with these new commands here)
(And practice with others)
Let's access the Linux command line on a local Debian test server that
sits in my office, just down the coast from Silicon Valley.
The way we normally start is by using
whatis to define commands we'll use
during the tutorial, so pause here if that helps.
paul@fullstack:~$ whatis printenv set export echo whatis ls clear less man
printenv (1) - print all or part of the environment
echo (1) - display a line of text
whatis (1) - display one-line manual page descriptions
ls (1) - list directory contents
clear (1) - clear the terminal screen
less (1) - opposite of more
man (1) - an interface to the on-line reference manuals
man (7) - macros to format man pages
set: nothing appropriate
export: nothing appropriate
I'll expand on this later, but please note,
export are builtins, and don't have
their own man pages, thus no
whatis entry here.
A good way to think of the environment is as a bunch of variables
easily accessed by system memory.
Step 2 - The Two Parts of the Linux Environment
Let's cover the two parts of the environment, starting with the easier
one, shell variables, and then environment
Linux shell variables
On our Linux operating system, we use the shell program
bash. There are others, but
bash is the most common and the
default with most Linux distributions.
paul@fullstack:~$ bash --help
GNU bash, version 4.3.30(1)-release-(x86_64-pc-linux-gnu)
Usage: bash [GNU long option] [option] ...
bash [GNU long option] [option] script-file ...
GNU long options:
-ilrsD or -c command or -O shopt_option (invocation only)
-abefhkmnptuvxBCHP or -o option
Type `bash -c "help set"' for more information about shell options.
Type `bash -c help' for more information about shell builtin commands.
Use the `bashbug' command to report bugs.
I bring this up because bash has its
own variables. The command printenv
is used to view them individually, like my
paul@fullstack:~$ printenv HOME
Or all together (use printenv) without
options and arguments, and there they are.
So environment variables encompass both the shell variables, we just
covered, plus a whole lot more, including the advanced shell functions,
that you see here, and this file goes on and on and on.
Okay, so set is a builtin, and we
covered builtins in tutorial 15, by using the
man page for
paul@fullstack:~$ man bash
BASH(1) General Commands Manual BASH(1)
bash - GNU Bourne-Again SHell
bash [options] [command_string | file]
Bash is Copyright (C) 1989-2013 by the Free Software Foundation, Inc.
Bash is an sh-compatible command language interpreter that executes commands read
from the standard input or from a file. Bash also incorporates useful features from
the Korn and C shells (ksh and csh).
Bash is intended to be a conformant implementation of the Shell and Utilities por‐
tion of the IEEE POSIX specification (IEEE Standard 1003.1). Bash can be configured
to be POSIX-conformant by default.
All of the single-character shell options documented in the description of the set
builtin command can be used as options when the shell is invoked. In addition, bash
interprets the following options when it is invoked:
-c If the -c option is present, then commands are read from the first non-
option argument command_string. If there are arguments after the com‐
mand_string, they are assigned to the positional parameters, starting with
Manual page bash(1) line 1 (press h for help or q to quit)
And because it's 5,000 lines long, type
3440g to zoom to the section on
builtins for help.
SHELL BUILTIN COMMANDS
Unless otherwise noted, each builtin command documented in this section as accepting
options preceded by - accepts -- to signify the end of the options. The :, true,
false, and test builtins do not accept options and do not treat -- specially. The
exit, logout, break, continue, let, and shift builtins accept and process arguments
beginning with - without requiring --. Other builtins that accept arguments but are
not specified as accepting options interpret arguments beginning with - as invalid
options and require -- to prevent this interpretation.
No effect; the command does nothing beyond expanding arguments and performing
any specified redirections. A zero exit code is returned.
. filename [arguments]
source filename [arguments]
Read and execute commands from filename in the current shell environment and
return the exit status of the last command executed from filename. If file
name does not contain a slash, filenames in PATH are used to find the direc
tory containing filename. The file searched for in PATH need not be exe
cutable. When bash is not in posix mode, the current directory is searched
if no file is found in PATH. If the sourcepath option to the shopt builtin
command is turned off, the PATH is not searched. If any arguments are sup
plied, they become the positional parameters when filename is executed. Oth
erwise the positional parameters are unchanged. The return status is the
status of the last command exited within the script (0 if no commands are
executed), and false if filename is not found or cannot be read.
alias [-p] [name[=value] ...]
Alias with no arguments or with the -p option prints the list of aliases in
Manual page bash(1) line 3440 (press h for help or q to quit)
Step 4 - Practice Viewing and Setting Variables
How to Create Variables
Okay, quickly, let's look at the command
export. It's another builtin, and is
used to pass variables to other shell processes.
Let's create a new variable, and assign it to my 2nd favorite word in
the English language.