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
-
Use the
export
command to add a new directory to your path (this will not survive a reboot). -
Use the
unset
command to delete thePATH
variable. Try running a command (likesudo cat /etc/shadow
) usingsudo
. What happened? Why? (Exiting your shell will return you to your original state.)
Explorational Exercises
-
Search the internet to find and explore the complete list of special characters.
-
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
andset
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
-
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 usingexport PATH="/home/yourname/myfiles:$PATH"
. Create a simple script in themyfiles/
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 calledmyfiles
.$ touch myfiles/myscript.sh $ echo '#!/bin/bash' >> myfiles/myscript.sh $ echo 'echo Hello' >> myfiles/myscript.sh $ chmod +x myfiles/myscript.sh $ myscript.sh Hello
-
Use the
unset
command to delete thePATH
variable. Try running a command (likesudo cat /etc/shadow
) usingsudo
. 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 yourPATH
usingexport
or by simply exiting from the shell.
Answers to Explorational Exercises
-
Search the internet to find and explore the complete list of special characters.
Here is a list:
& ; | * ? " ' [ ] ( ) $ < > { } # / \ ! ~
. -
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