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
variables.
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)
printenv
set
export
echo
(And practice with others)
whatis
ls
clear
less
man
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,
set and
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
variables.
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:
--debug
--debugger
--dump-po-strings
--dump-strings
--help
--init-file
--login
--noediting
--noprofile
--norc
--posix
--rcfile
--restricted
--verbose
--version
Shell 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
HOME directory.
paul@fullstack:~$ printenv HOME
/home/paul
Or all together (use printenv) without
options and arguments, and there they are.
Granted, many are beyond our scope here as beginners, but we should be
able to decipher a few. Shift+PgUp
and SHELL here points to our shell program
bash.
Here's the USER paul, then
Shift+PgDn and PWD is
the current working directory (so this variable will change every time
you change the working directory).
LS_COLORS sets colors depending on the file type and
context when you use the ls command.
So looking at the 135 programs in this system's
/bin directory we can see colors by
file type.
paul@fullstack:~$ ls /bin
(note: 2 lines trimmed and this is in color on a monitor)
bzexe gunzip netcat system-tmpfiles
bzfgrep gzexe netstat system-tty-ask-password-agent
bzgrep gzip nisdomainname tailf
bzip2 hostname open tar
bzip2recover ip openvt tempfile
bzless journalctl pidof touch
bzmore kbd_mode ping true
cat kill ping6 udevadm
chacl kmod ps umount
chgrp less pwd uname
chmod lessecho rbash uncompress
chown lessfile readlink unicode_start
chvt lesskey rm vdir
cp lesspipe rmdir wdctl
cpio ln rnano which
dash loadkeys run-parts ypdomainname
date login sed zcat
dd loginctl setfacl zcmp
df ls setfont zdiff
dir lsblk setupcon zegrep
dmesg lsmod sh zfgrep
dnsdomainname machinectl sh.distrib zforce
domainname mkdir sleep zgrep
dumpkeys mknod ss zless
echo mktemp stty zmore
efibootmgr more su znew
egrep mount sync
paul@fullstack:~$ clear
Oh, and hold on to that thought because we'll do something
similar in the next video, with the text editor
nano, making it more appealing.
Linux environment variables
Let's move on to environment variables and to view those, we'll use
the set command without options and
arguments.
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
bash.
paul@fullstack:~$ man bash
BASH(1) General Commands Manual BASH(1)
NAME
bash - GNU Bourne-Again SHell
SYNOPSIS
bash [options] [command_string | file]
COPYRIGHT
Bash is Copyright (C) 1989-2013 by the Free Software Foundation, Inc.
DESCRIPTION
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.
OPTIONS
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.
: [arguments]
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.
The next command is echo and it is
useful for displaying both text and variables, but to
echo variables you need to use the
dollar sign $.
paul@fullstack:~$ export text
paul@fullstack:~$ printenv USER
paul
paul@fullstack:~$ echo USER
USER
paul@fullstack:~$ echo $USER
paul
And as a summary, to see how well you've been listening
;) see if you
can follow the logic here for how echo
works with variables, while I'm coding up and little five-part
command using a variable.
If you learned something today, please consider subscribing. Thank you, paul
Step 5 - Next: Linux Environment Variables
Next, we'll look at our software stack.
Client : HTML, CSS, JavaScript
Software : Python Scientific Stack
Data : PostgreSQL, MySQL
OS : Linux (command line), Debian
It's early in our journey.
We have one more tutorial until we finish this project, then we will
hit the software layer and play with Python and Math. Before that
we will discuss text editors and configure
nano.
Have a nice day.
What's Next?
Learn faster by supplementing this text with video on our YouTube
Channel. Subscribe to be notified of new content.
To access all tutorials, click Outline.
To understand permissions in Linux, click Back.
Fight big company greed and control, support the little guy, click Tip.
To set syntax highlighting and colors in nano, click Next.