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
2.1 Lesson 2
Topic 1: The Linux Community and a Career in Open Source
1.1 Linux Evolution and Popular Operating Systems
  • 1.1 Lesson 1
1.2 Major Open Source Applications
  • 1.2 Lesson 1
1.3 Open Source Software and Licensing
  • 1.3 Lesson 1
1.4 ICT Skills and Working in Linux
  • 1.4 Lesson 1
Topic 2: Finding Your Way on a Linux System
2.1 Command Line Basics
  • 2.1 Lesson 1
  • 2.1 Lesson 2
2.2 Using the Command Line to Get Help
  • 2.2 Lesson 1
2.3 Using Directories and Listing Files
  • 2.3 Lesson 1
  • 2.3 Lesson 2
2.4 Creating, Moving and Deleting Files
  • 2.4 Lesson 1
Topic 3: The Power of the Command Line
3.1 Archiving Files on the Command Line
  • 3.1 Lesson 1
3.2 Searching and Extracting Data from Files
  • 3.2 Lesson 1
  • 3.2 Lesson 2
3.3 Turning Commands into a Script
  • 3.3 Lesson 1
  • 3.3 Lesson 2
Topic 4: The Linux Operating System
4.1 Choosing an Operating System
  • 4.1 Lesson 1
4.2 Understanding Computer Hardware
  • 4.2 Lesson 1
4.3 Where Data is Stored
  • 4.3 Lesson 1
  • 4.3 Lesson 2
4.4 Your Computer on the Network
  • 4.4 Lesson 1
Topic 5: Security and File Permissions
5.1 Basic Security and Identifying User Types
  • 5.1 Lesson 1
5.2 Creating Users and Groups
  • 5.2 Lesson 1
5.3 Managing File Permissions and Ownership
  • 5.3 Lesson 1
5.4 Special Directories and Files
  • 5.4 Lesson 1
How to get certified
  1. Topic 2: Finding Your Way on a Linux System
  2. 2.1 Command Line Basics
  3. 2.1 Lesson 2

2.1 Lesson 2

Certificate:

Linux Essentials

Version:

1.6

Topic:

2 Finding Your Way on a Linux System

Objective:

2.1 Command Line Basics

Lesson:

2 of 2

Introduction

All shells manage a set of status information throughout the shell sessions. This runtime information may change during the session and influences how the shell behaves. This data is also used by programs to determinate aspects of the system’s configuration. Most of this data is stored in so-called variables, which we will cover in this lesson.

Variables

Variables are pieces of storage for data, such as text or numbers. Once set, a variable’s value can be accessed at a later time. Variables have a name which allows accessing a specific variable, even when the variable’s content changes. They are a very common tool in most programming languages.

In most Linux shells, there are two types of variables:

Local variables

These variables are available to the current shell process only. If you create a local variable and then start another program from this shell, the variable is not accessible to that program anymore. Because they are not inherited by sub processes, these variables are called local variables.

Environment variables

These variables are available both in a specific shell session and in sub processes spawned from that shell session. Theses variables can be used to pass configuration data to commands which are run. Because these programs can access these variables, they are called environment variables. The majority of the environment variables are in capital letters (e.g. PATH, DATE, USER). A set of default environment variables provide, for example, information about the user’s home directory or terminal type. Sometimes the complete set of all environment variables is referred to as the environment.

These types of variables are also known as variable scope.

Note

Variables are not persistent. When the shell in which they were set is closed, all variables and their contents are lost. Most shells provide configuration files that contain variables which are set whenever a new shell is started. Variables that should be set permanently must be added to one of these configuration files.

Manipulating Variables

As a system administrator, you will need to create, modify or remove both local and environment variables.

Working with Local Variables

You can set up a local variable by using the = (equal) operator. A simple assignment will create a local variable:

$ greeting=hello
Note

Don’t put any space before or after the = operator.

You can display any variable using the echo command. The command usually displays the text in the argument section:

$ echo greeting
greeting

In order to access the value of the variable you will need to use $ (dollar sign) in front of the variable’s name.

$ echo $greeting
hello

As it can be seen, the variable has been created. Now open another shell and try to display the contents of the variable created.

$ echo $greeting

Nothing is displayed. This illustrates, that variables always exist in a specific shell only.

To verify that the variable is actually a local variable, try to spawn a new process and check if this process can access the variable. We can do so by starting another shell and let this shell run the echo command. As the new shell is run in a new process, it won’t inherit local variables from its parent process:

$ echo $greeting world
hello world
$ bash -c 'echo $greeting world'
world
Note

Make sure to use single quotes in the example above.

In order to remove a variable, you will need to use the command unset:

$ echo $greeting
hey
$ unset greeting
$ echo $greeting
Note

unset requires the name of the variable as an argument. Therefore you may not add $ to the name, as this would resolve the variable and pass the variable’s value to unset instead of the variable’s name.

Working with Global Variables

To make a variable available to subprocesses, turn it from a local into an environment variable. This is done by the command export. When it is invoked with the variable name, this variable is added to the shell’s environment:

$ greeting=hello
$ export greeting
Note

Again, make sure to not use $ when running export as you want to pass the name of the variable instead of its contents.

An easier way to create the environment variable is to combine both of the above methods, by assigning the variable value in the argument part of the command.

$ export greeting=hey

Let’s check again if the variable is accessible to subprocesses:

$ export greeting=hey
$ echo $greeting world
hey world
$ bash -c 'echo $greeting world'
hey world

Another way to use environment variables is to use them in front of commands. We can test this with the environment variable TZ which holds the timezone. This variable is used by the command date to determine which timezone’s time to display:

$ TZ=EST date
Thu 31 Jan 10:07:35 EST 2019
$ TZ=GMT date
Thu 31 Jan 15:07:35 GMT 2019

You can display all environment variables using the env command.

The PATH Variable

The PATH variable is one of the most important environment variables in a Linux system. It stores a list of directories, separated by a colon, that contain executable programs eligible as commands from the Linux shell.

$ echo $PATH
/home/user/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

To append a new directory to the variable, you will need to use the colon sign (:).

$ PATH=$PATH:new_directory

Here an example:

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
$ PATH=$PATH:/home/user/bin
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/user/bin

As you see, $PATH is used in the new value assigned to PATH. This variable is resolved during the command execution and makes sure that the original content of the variable is preserved. Of course, you can use other variables in the assignment as well:

$ mybin=/opt/bin
$ PATH=$PATH:$mybin
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/user/bin:/opt/bin

The PATH variable needs to be handled with caution, as it is crucial for working on the command line. Let’s consider the following PATH variable:

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

To find out how the shell invokes a specific command, which can be run with the command’s name as argument. We can, for example, try to find out where nano is stored:

$ which nano
/usr/bin/nano

As it can be seen, the nano executable is located within the /usr/bin directory. Let’s remove the directory from the variable and check to see if the command still works:

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

Let’s look up the nano command again:

$ which nano
which: no nano in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/sbin:/bin:/usr/games)

As it can be seen, the command is not found, therefore not executed. The error message also explains the reason why the command was not found and in what locations it was searched.

Let’s add back the directories and try running the command again.

$ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
$ which nano
/usr/bin/nano

Now our command works again.

Tip

The order of elements in PATH also defines the lookup order. The first matching executable found while going through the paths is executed.

Guided Exercises

  1. Create a local variable number.

  2. Create an environment variable ORDER, using one of the two above methods.

  3. Display both the variable names and their contents.

  4. What are the scopes of the previously created variables?

Explorational Exercises

  1. Create a local variable nr_files and assign the number of lines found in the /etc/passwd file. Hint: Look into the command wc and command substitution and don’t forget about quotation marks.

  2. Create an environment variable ME. Assign the USER variable’s value to it.

  3. Append the HOME variable’s value to ME, having the : delimiter. Display the contents of the ME variable.

  4. Using the date example above, create a variable called today and assign the date for one of the time zones.

  5. Create another variable called today1 and assign the system’s date to it.

Summary

In this lab you learned:

  • Types of variables

  • How to create variables

  • How to manipulate variables

Commands used in the exercises:

env

Display the current environment.

echo

Output text.

export

Make local variables available to subprocesses.

unset

Remove a variable.

Answers to Guided Exercises

  1. Create a local variable number.

    $ number=5
  2. Create an environment variable ORDER, using one of the two above methods.

    $ export ORDER=desc
  3. Display both the variable names and their contents.

    $ echo number
    number
    $ echo ORDER
    ORDER
    $ echo $number
    5
    $ echo $ORDER
    desc
  4. What are the scopes of the previously created variables?

    • The scope of the local variable number is the current shell only.

    • The scope of the environment variable ORDER is the current shell and all the subshells generated by it.

Answers to Explorational Exercises

  1. Create a local variable nr_files and assign the number of lines found in the /etc/passwd file. Hint: Look into the command wc and command substitution and don’t forget about quotation marks.

    $ nr_files=`wc -l /etc/passwd`
  2. Create an environment variable ME. Assign the USER variable’s value.

    $ export ME=$USER
  3. Append the HOME variable value to ME, having the : delimiter. Display the contents of the ME variable.

    $ ME=$ME:$HOME
    $ echo $ME
    user:/home/user
  4. Using the date example above, create a variable called today and assign the date for one of the time zones.

    The following use the GMT and EST time zones as an example, but any time zone selection is valid.

    $ today=$(TZ=GMT date)
    $ echo $today
    Thu 31 Jan 15:07:35 GMT 2019

    or

    $ today=$(TZ=EST date)
    $ echo $today
    Thu 31 Jan 10:07:35 EST 2019
  5. Create another variable called today1 and assign the system’s date to it.

    Assuming that you are in GMT:

    $ today1=$(date)
    $ echo $today1
    Thu 31 Jan 10:07:35 EST 2019

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

2.2 Using the Command Line to Get Help (2.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.

© 2022 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–2022 The Linux Professional Institute Inc. All rights reserved.