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.
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 |
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
|
|
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 |
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 |
Guided Exercises
-
Create a local variable
number
. -
Create an environment variable
ORDER
, using one of the two above methods. -
Display both the variable names and their contents.
-
What are the scopes of the previously created variables?
Explorational Exercises
-
Create a local variable
nr_files
and assign the number of lines found in the/etc/passwd
file. Hint: Look into the commandwc
and command substitution and don’t forget about quotation marks. -
Create an environment variable
ME
. Assign theUSER
variable’s value to it. -
Append the
HOME
variable’s value toME
, having the:
delimiter. Display the contents of theME
variable. -
Using the date example above, create a variable called
today
and assign the date for one of the time zones. -
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
-
Create a local variable
number
.$ number=5
-
Create an environment variable
ORDER
, using one of the two above methods.$ export ORDER=desc
-
Display both the variable names and their contents.
$ echo number number $ echo ORDER ORDER $ echo $number 5 $ echo $ORDER desc
-
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
-
Create a local variable
nr_files
and assign the number of lines found in the/etc/passwd
file. Hint: Look into the commandwc
and command substitution and don’t forget about quotation marks.$ nr_files=`wc -l /etc/passwd`
-
Create an environment variable
ME
. Assign theUSER
variable’s value.$ export ME=$USER
-
Append the
HOME
variable value toME
, having the:
delimiter. Display the contents of theME
variable.$ ME=$ME:$HOME $ echo $ME user:/home/user
-
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
-
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