Linux Professional Institute Learning Logo.
Skip to main content
  • Home
    • All Resources
    • LPI Learning Materials
    • Become a Contributor
    • Publishing Partners
    • Become a Publishing Partner
    • About
    • FAQ
    • Contributors
    • Roadmap
    • Contact
  • LPI.org
103.1 Lesson 2
Topic 101: System Architecture
101.1 Determine and configure hardware settings
  • 101.1 Lesson 1
101.2 Boot the system
  • 101.2 Lesson 1
101.3 Change runlevels / boot targets and shutdown or reboot system
  • 101.3 Lesson 1
Topic 102: Linux Installation and Package Management
102.1 Design hard disk layout
  • 102.1 Lesson 1
102.2 Install a boot manager
  • 102.2 Lesson 1
102.3 Manage shared libraries
  • 102.3 Lesson 1
102.4 Use Debian package management
  • 102.4 Lesson 1
102.5 Use RPM and YUM package management
  • 102.5 Lesson 1
102.6 Linux as a virtualization guest
  • 102.6 Lesson 1
Topic 103: GNU and Unix Commands
103.1 Work on the command line
  • 103.1 Lesson 1
  • 103.1 Lesson 2
103.2 Process text streams using filters
  • 103.2 Lesson 1
103.3 Perform basic file management
  • 103.3 Lesson 1
  • 103.3 Lesson 2
103.4 Use streams, pipes and redirects
  • 103.4 Lesson 1
  • 103.4 Lesson 2
103.5 Create, monitor and kill processes
  • 103.5 Lesson 1
  • 103.5 Lesson 2
103.6 Modify process execution priorities
  • 103.6 Lesson 1
103.7 Search text files using regular expressions
  • 103.7 Lesson 1
  • 103.7 Lesson 2
103.8 Basic file editing
  • 103.8 Lesson 1
Topic 104: Devices, Linux Filesystems, Filesystem Hierarchy Standard
104.1 Create partitions and filesystems
  • 104.1 Lesson 1
104.2 Maintain the integrity of filesystems
  • 104.2 Lesson 1
104.3 Control mounting and unmounting of filesystems
  • 104.3 Lesson 1
104.5 Manage file permissions and ownership
  • 104.5 Lesson 1
104.6 Create and change hard and symbolic links
  • 104.6 Lesson 1
104.7 Find system files and place files in the correct location
  • 104.7 Lesson 1
How to get certified
  1. Topic 103: GNU and Unix Commands
  2. 103.1 Work on the command line
  3. 103.1 Lesson 2

103.1 Lesson 2

Certificate:

LPIC-1

Version:

5.0

Topic:

103 GNU and Unix Commands

Objective:

103.1 Work on the command line

Lesson:

2 of 2

Introduction

An operating system environment includes the basic tools — like command line shells and sometimes a GUI — that you will need in order to get stuff done. But your environment will also come with a catalog of shortcuts and preset values. Here is where we will learn how to list, invoke, and manage those values.

Finding Your Environment Variables

So just how do we identify the current values for each of our environment variables? One way is through the env command:

$ env
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
XDG_RUNTIME_DIR=/run/user/1000
XAUTHORITY=/run/user/1000/gdm/Xauthority
XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/etc/xdg
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
GJS_DEBUG_TOPICS=JS ERROR;JS LOG
[...]

You will get a lot of output — much more than what is included in the above excerpt. But for now note the PATH entry, which contains the directories where your shell (and other programs) will look for other programs without having to specify a complete path. With that set, you could run a binary program that lives, say, in /usr/local/bin from within your home directory and it would run just as though the file was local.

Let us change the subject for a moment. The echo command will print to the screen whatever you tell it to. Believe it or not, there will be many times when getting echo to literally repeat something will be very useful.

$ echo "Hi. How are you?"
Hi. How are you?

But there is something else you can do with echo. When you feed it the name of an environment variable — and tell it that this is a variable by prefixing the variable name with a $ — then, instead of just printing the variable’s name, the shell will expand it giving you the value. Not sure whether your favorite directory is currently in the path? You can quickly check by running it through echo:

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

Creating New Environment Variables

You can add your own custom variables to your environment. The simplest way is to use the = character. The string to the left will be the name of your new variable, and the string to the right will be its value. You can now feed the variable name to echo to confirm it worked:

$ myvar=hello
$ echo $myvar
hello
Note
Notice that there is no space on either side of the equal sign during variable assignment.

But did it really work? Type bash into the terminal to open a new shell. This new shell looks exactly like the one you were just in, but it is actually a child of the original one (which we call the parent). Now, inside this new child shell, try to get echo to do its magic the way it did before. Nothing. What’s going on?

$ bash
$ echo $myvar

$

A variable you create the way we just did is only going to be available locally — within the immediate shell session. If you start up a new shell — or close down the session using exit — the variable will not go along with you. Typing exit here will take you back to your original parent shell which, right now, is where we want to be. You can run echo $myvar once again if you like just to confirm that the variable is still valid. Now type export myvar to pass the variable to any child shells that you may subsequently open. Try it out: type bash for a new shell and then echo:

$ exit
$ export myvar
$ bash
$ echo $myvar
hello

All this may feel a bit silly when we are creating shells for no real purpose. But understanding how shell variables are propagated through your system will become very important once you start writing serious scripts.

Deleting Environment Variables

Want to know how to clean up all those ephemeral variables you have created? One way is to simply close your parent shell — or reboot your computer. But there are simpler ways. Like, for instance, unset. Typing unset (without the $) will kill the variable. echo will now prove that.

$ unset myvar
$ echo $myvar

$

If there is an unset command, then you can bet there must be a set command to go with it. Running set by itself will display lots of output, but it is really not all that different from what env gave you. Look at the first line of output you will get when you filter for PATH:

$ set | grep PATH
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
[...]

What is the difference between set and env? For our purposes, the main thing is that set will output all variables and functions. Let us illustrate that. We will create a new variable called mynewvar and then confirm it is there:

$ mynewvar=goodbye
$ echo $mynewvar
goodbye

Now, running env while using grep to filter for the string mynewvar will not display any output. But running set the same way will show us our local variable.

$ env | grep mynewvar

$ set | grep mynewvar
mynewvar=goodbye

Quoting to Escape Special Characters

Now is as good a time as any other to introduce you to the problem of special characters. Alphanumeric characters (a-z and 0-9) will normally be read literally by Bash. If you try to create a new file called myfile you would just type touch followed by myfile and Bash will know what to do with it. But if you want to include a special character in your filename, you will need to do a bit more work.

To illustrate this, we will type touch and follow it by the title: my big file. The problem is that there are two spaces there between words which Bash will interpret. While, technically, you would not call a space a “character,” it is like one in the sense that Bash will not read it literally. If you list the contents of your current directory, rather than one file called my big file, you will see three files named, respectively, my, big, and file. That is because Bash thought you wanted to create multiple files whose names you were passing in a list:

$ touch my big file
$ ls
my big file

The spaces will be interpreted the same way if you delete (rm) the three files all in one command:

$ rm my big file

Now let us try it the right way. Type touch and the three parts of your filename but this time enclose the name in quotation marks. This time it worked. Listing the directory contents will show you a single file with the proper name.

$ touch "my big file"
$ ls
'my big file'

There are other ways to get the same effect. Single quotes, for instance, work just as well as double quotes. (Note that single quotes will preserve the literal value of all characters, while double quotes will preserve all characters except for $, `, \ and, on certain cases, !.)

$ rm 'my big file'

Prepending each special character with the backslash will “escape” the specialness of the character and cause Bash to read it literally.

$ touch my\ big\ file

Guided Exercises

  1. Use the export command to add a new directory to your path (this will not survive a reboot).

  2. Use the unset command to delete the PATH variable. Try running a command (like sudo cat /etc/shadow) using sudo. What happened? Why? (Exiting your shell will return you to your original state.)

Explorational Exercises

  1. Search the internet to find and explore the complete list of special characters.

  2. Try running commands using strings made up of special characters and using various methods to escape them. Are there differences between the way those methods behave?

Summary

In this lesson, you learned:

  • How to identify your system’s environment variables.

  • How to create your own environment variables and export them to other shells.

  • How to remove environment variables and how to use both the env and set commands.

  • How to escape special characters so Bash will read them literally.

The following commands were discussed in this lesson:

echo

Print input strings and variables.

env

Understand and modify your environment variables.

export

Pass an environment variable to child shells.

unset

Unset values and attributes of shell variables and functions.

Answers to Guided Exercises

  1. Use the export command to add a new directory to your path (this will not survive a reboot).

    You can temporarily add a new directory (perhaps one called myfiles that lives in your home directory) to your path using export PATH="/home/yourname/myfiles:$PATH". Create a simple script in the myfiles/ directory, make it executable, and try to run it from a different directory. These commands assume you’re in your home directory which contains a directory called myfiles.

    $ touch myfiles/myscript.sh
    $ echo '#!/bin/bash' >> myfiles/myscript.sh
    $ echo 'echo Hello' >> myfiles/myscript.sh
    $ chmod +x myfiles/myscript.sh
    $ myscript.sh
    Hello
  2. Use the unset command to delete the PATH variable. Try running a command (like sudo cat /etc/shadow) using sudo. What happened? Why? (Exiting your shell will return you to your original state.)

    Typing unset PATH will erase the current path settings. Trying to invoke a binary without its absolute address will fail. For that reason, trying to run a command using sudo (which itself is a binary program located in /usr/bin/sudo) will fail — unless you specify the absolute location, as in: /usr/bin/sudo /bin/cat /etc/shadow. You can reset your PATH using export or by simply exiting from the shell.

Answers to Explorational Exercises

  1. Search the internet to find and explore the complete list of special characters.

    Here is a list: & ; | * ? " ' [ ] ( ) $ < > { } # / \ ! ~.

  2. Try running commands using strings made up of special characters and using various methods to escape them. Are there differences between the way those methods behave?

    Escaping using " characters will preserve the special values of the dollar sign, a backtick, and the backslash. Escaping using a ' character, on the other hand, will render all characters as literal.

    $ echo "$mynewvar"
    goodbye
    $ echo '$mynewvar'
    $mynewvar

Linux Professional Insitute Inc. All rights reserved. Visit the Learning Materials website: https://learning.lpi.org
This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.

Next Lesson

103.2 Process text streams using filters (103.2 Lesson 1)

Read next lesson

Linux Professional Insitute Inc. All rights reserved. Visit the Learning Materials website: https://learning.lpi.org
This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.

LPI is a non-profit organization.

© 2023 Linux Professional Institute (LPI) is the global certification standard and career support organization for open source professionals. With more than 200,000 certification holders, it's the world’s first and largest vendor-neutral Linux and open source certification body. LPI has certified professionals in over 180 countries, delivers exams in multiple languages, and has hundreds of training partners.

Our purpose is to enable economic and creative opportunities for everybody by making open source knowledge and skills certification universally accessible.

  • LinkedIn
  • flogo-RGB-HEX-Blk-58 Facebook
  • Twitter
  • Contact Us
  • Privacy and Cookie Policy

Spot a mistake or want to help improve this page? Please let us know.

© 1999–2023 The Linux Professional Institute Inc. All rights reserved.