FactorPad
Build a Better Process

What Are Linux Environment Variables?

The environment is what is going on behind the scenes. Here we learn how to examine it.
  1. The environment - Define the environment.
  2. Two parts - Review shell variables and environment variables with printenv and the builtin set command.
  3. Variables - Learn to add and edit variables with the export command.
  4. Practice - Change variables and view them with echo.
  5. Next: nano config - Set syntax highlighting and colors in nano.
face pic by Paul Alan Davis, CFA
Updated: February 21, 2021
The Linux environment is confusing at the beginner level, so see this as a quick flyby.

Outline Back Tip Next

/ factorpad.com / tech / full-stack / linux-environment.html


An ad-free and cookie-free website.


Learn how to access and modify the Linux environment

Beginner

Video Tutorial

Videos can also be accessed from our Full Stack Playlist 2 on YouTube.

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)

(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, 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.

paul@fullstack:~$ printenv TERM=xterm SHELL=/bin/bash SSH_CLIENT=192.168.0.10 56083 22 SSH_TTY=/dev/pts/0 USER=paul LS_COLORS= (trimmed but examples include *.zip=01;31:*.deb=01;31:*.rpm=01;31:) MAIL=/var/mail/paul PATH=/home/paul/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin PWD=/home/paul/public_html/factorpad/tech/full-stack LANG=en_US.UTF-8 SHLVL=1 HOME=/home/paul LOGNAME=paul SSH_CONNECTION=192.168.0.7 49483 192.168.0.6 22 _=/usr/bin/printenv OLDPWD=/home/paul/public_html/factorpad

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.

paul@fullstack:~$ set | less

Pipe it in to less because the output is so long.

BASH=/bin/bash BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore: histappend:interactive_comments:login_shell:progcomp:promptvars:sourcepath BASH_ALIASES=() BASH_ARGC=() BASH_ARGV=() BASH_CMDS=() BASH_COMPLETION_COMPAT_DIR=/etc/bash_completion.d BASH_LINENO=() BASH_REMATCH=() BASH_SOURCE=() BASH_VERSINFO=([0]="4" [1]="3" [2]="30" [3]="1" [4]="release" [5]="x86_64-pc-linux-gnu") BASH_VERSION='4.3.30(1)-release' COLUMNS=94 COMP_WORDBREAKS=$' \t\n"\'><=;|&(:' DIRSTACK=() EUID=1000 GROUPS=() HISTCONTROL=ignoreboth HISTFILE=/home/paul/.bash_history HISTFILESIZE=2000 HISTSIZE=1000 HOME=/home/paul HOSTNAME=fullstack HOSTTYPE=x86_64 IFS=$' \t\n' LANG=en_US.UTF-8 LINES=30 LOGNAME=paul (1639 lines trimmed)

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.

paul@fullstack:~$ VAR1=sabbatical paul@fullstack:~$ echo $VAR1 sabbatical

With echo it works (meaning the shell stored the variable), but will it work if we use printenv?

paul@fullstack:~$ printenv VAR1 paul@fullstack:~$

No, because we need to export the variable to make it available for other bash processes. As you can see here.

paul@fullstack:~$ export VAR1 paul@fullstack:~$ printenv VAR1 sabbatical

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.

paul@fullstack:~$ clear; sleep 1; echo "If you learned something today, please consider subscribing" Thank you, $USER; sleep 3; clear

(Wait for it).

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.

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.

Outline Back Tip Next

/ factorpad.com / tech / full-stack / linux-environment.html


linux environment
set linux environment variables
linux export command
linux set command
linux echo
linux printenv
linux variable
linux show environment variables
shell variable
what are linux environment variables
export environment variable
bash variables
linux env
linux tutorial

A newly-updated free resource. Connect and refer a friend today.