Using The Shell
Using The Shell
1 Introduction
The definition of the word Linux depends on the context in which it is used. Linux means
the kernel of the system, which is the central controller of everything that happens on
the computer (more on this later). People that say their computer “runs Linux” usually
refer to the kernel and suite of tools that come with it (called the distribution). If
someone says they have “Linux experience”, they are most likely talking about the
programs themselves. However, they might also be talking about knowing how to add
and partition a new disk or even fine-tune the kernel. Each of these components will be
investigated so that you understand exactly what roles each plays.
What about UNIX? UNIX was originally an operating system developed at AT&T Bell
Labs in the 1970’s. It was modified and forked (that is, people modified it and those
modifications served as the basis for other systems) such that now there are many
different variants of UNIX. However, UNIX is now both a trademark and a specification,
owned by an industry consortium called the Open Group. Only software that has been
certified by the Open Group may call itself UNIX. Despite adopting all the requirements
of the UNIX specification, Linux has not been certified, so Linux really isn’t UNIX! It’s
just… UNIX-like.
1.1.2 Applications
Like an air traffic controller, the kernel is not useful without something to control. If the
kernel is the tower, the applications are the airplanes. Applications make requests to the
kernel and receive resources, such as memory, CPU, and disk, in return. The kernel also
abstracts the complicated details away from the application. The application doesn’t
know if a block of disk is on a solid-state drive from manufacturer A, a spinning metal
hard drive from manufacturer B, or even a network file share. Applications just follow
the kernel’s Application Programming Interface (API) and in return don’t have to worry
about the implementation details.
When we, as users, think of applications, we tend to think of word processors, web
browsers, and email clients. The kernel doesn’t care if it is running something that’s
user facing, a network service that talks to a remote computer, or an internal task. So,
from this we get an abstraction called a process. A process is just one task that is loaded
and tracked by the kernel. An application may even need multiple processes to function,
so the kernel takes care of running the processes, starting and stopping them as
requested, and handing out system resources.
1.3 Shell
An operating system provides at least one shell or interface; this allows you to tell the
computer what to do. A shell is sometimes called an interpreter because it takes the
commands that a user issues and interprets them into a form that the kernel can then
execute on the hardware of the computer. The two most common types of shells are the
Graphical User Interface (GUI) and Command Line Interface (CLI).
Windows® typically use a GUI shell, primarily using the mouse to indicate what you
want done. While using an operating system in this way might be considered easy, there
are many advantages to using a CLI, including:
Command Repetition: In a GUI shell, there is no easy way to repeat a previous
command. In a CLI there is an easy way to repeat (and also modify) a previous
command.
Command Flexibility: The GUI shell provides limited flexibility in the way the
command executes. In a CLI, options are specified with commands to provide a much
more flexible and powerful interface.
Resources: A GUI shell typically uses a vast amount of resources (RAM, CPU, etc.). This
is because a great deal of processing power and memory is needed to display graphics.
By contrast, a CLI uses very little system resources, allowing more of these resources to
be available to other programs.
Scripting: In a GUI shell, completing multiple tasks often requires multiple mouse
clicks. With a CLI, a script can be created to execute many complex operations by typing
just a single "command": the name of the script. A script is a series of commands placed
into a single file. When executed, the script runs all of the commands in the file.
Remote Access: While it is possible to remotely execute commands in a GUI shell, this
feature isn't typically set up by default. With a CLI shell, gaining access to a remote
machine is easy and typically available by default.
Development: Normally a GUI-based program takes more time for the developers to
create when compared to CLI-based programs. As a result, there are typically thousands
of CLI programs on a typical Linux OS while only a couple hundred programs in a
primarily GUI-based OS like Microsoft Windows®. More programs means more power
and flexibility.
The Microsoft Windows® Operating System was designed to primarily use the GUI
interface because of its simplicity, although there are several CLI interfaces available,
too. For simple commands, there is the Run dialog box, where you can type or browse to
the commands that you want to execute. If you want to type multiple commands or if
you want to see the output of the command, you can use the Command Prompt, also
called the DOS shell. Recently, Microsoft realized how important it is to have a powerful
command line environment and, as a result, has introduced the Powershell.
Like Windows™, Linux also has both a CLI and GUI. Unlike Windows™, Linux lets you
easily change the GUI shell (also called the desktop environment) that you want to use.
The two most common desktop environments for Linux are GNOME and KDE, however
there are many other GUI shells available.
To access the CLI from within the GUI on a Linux operating system, the user can open a
software program called a terminal. Linux can also be configured to only run the CLI
without the GUI; this is typically done on servers that don't require a GUI, primarily to
free up system resources.
[1] 108
The shell that your user account uses by default is set at the time your user account was
created. By default, many Linux distributions use bash for a new user's shell. An
administrator can use the usermod command to specify a different default shell after the
account has been created.
As a user, you can use the chsh command to change your default shell.
The location where the system stores the default shell for user accounts is
the /etc/passwd file.
Note: The usermod and chsh commands, as well as the /etc/passwdfile will be discussed in
greater detail later in this course.
Typically, a user learns one shell and sticks with that shell, however after you have
learned the basics of Linux, you may want to explore the features of other shells.
In the early days of computing, terminal devices were large machines that allowed users
to provide input through a keyboard and displayed output by printing on paper. Over
time, terminals evolved and their size shrank down into something that looked similar
to a desktop computer with a video display monitor for output and a keyboard for input.
Ultimately, with the introduction of personal computers, terminals became software
emulators of the actual hardware. Whatever you type in the terminal is interpreted by
your shell and translated into a form that can then be executed by the kernel of the
operating system.
If you are in a remote location, then pseudo-terminal connections can also be made
across the network using several techniques. Insecure connections could be made using
protocols such as telnet and programs such as rlogin, while secure connections can be
established using programs like putty and protocols such as ssh.
1.6 Filesystems
In addition to the kernel and the shell, the other major component of any operating
system is the filesystem. To the user, a filesystem is a hierarchy of directories and files
with the root / directory at the top of the directory tree. To the operating system, a
filesystem is a structure created on a disk partition consisting of tables defining the
locations of directories and files. In this course, you will learn about the different Linux
filesystems, filesystem benefits and how to create and manage filesystems using
commands like fsck, mount and other disk and filesystem management commands.
Consider This
While aliases will be covered in detail in a later section, this brief example may be
helpful in understanding the concept of commands.
An alias is essentially a nickname for another command or series of commands. For
example, the cal 2014 command will display the calendar for the year 2014. Suppose you
end up running this command often. Instead of executing the full command each time,
you can create an alias called mycal and run the alias, as demonstrated in the following
graphic:
sysadmin@localhost:~$ alias mycal="cal 2014"
sysadmin@localhost:~$ mycal
2014
January February March
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 1 1
5 6 7 8 9 10 11 2 3 4 5 6 7 8 2 3 4 5 6 7 8
12 13 14 15 16 17 18 9 10 11 12 13 14 15 9 10 11 12 13 14 15
19 20 21 22 23 24 25 16 17 18 19 20 21 22 16 17 18 19 20 21 22
26 27 28 29 30 31 23 24 25 26 27 28 23 24 25 26 27 28 29
30 31
Command files can also contain human-readable text in the form of script files. A script
file is a collection of commands that is typically executed at the command line.
The ability to create your own script files is a very powerful feature of the CLI. If you
have a series of commands that you regularly find yourself typing in order to
accomplish some task, then you can easily create a bash shell script to perform these
multiple commands by typing just one command: the name of your script file. You
simply need to place these commands into a file and make the file executable (more
details on this will be provided in a later unit).
Summary of Key Terms
Command: Something that a user types in a CLI that will result in an action taking place
on the system.
Compiled: The result of converting human-readable text code into system-readable
binary code.
Source Code: The original human-readable text code.
Script File: A text file that contains commands and has been made executable.
In other words, you type a command, followed by one or more options (which are not
always required) and one or more arguments before you press the Enter key. Although
there are some commands in Linux that aren’t entirely consistent with this syntax, most
commands use this syntax.
When typing a command that is to be executed, the first step is to type the name of the
command. The name of the command is often based on what it does or what the
developer who created the command thinks will best describe the command's function.
For example, the ls command displays a listing of information about files. Associating the
name of the command with something mnemonic for what it does may help you to
remember commands more easily.
You must remember that every part of the command is normally case-sensitive, so LS is
incorrect and will fail, but ls is correct and will succeed.
In the following example, the ls command is executed without any options or arguments,
which results in the current directory contents being displayed. Many commands, like
the ls command, can run successfully without any options or arguments, but be aware
that there are commands that require you to type more than just the command alone.
sysadmin@localhost:~$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos test
sysadmin@localhost:~$ ls -l
total 0
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Desktop
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Documents
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Downloads
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Music
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Pictures
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Public
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Templates
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Videos
drwxr-xr-x 1 sysadmin sysadmin 420 Sep 18 22:25 test
Note: More details will be provided in a later section regarding the function of
the ls command. For now, it is just being used to demonstrate how to execute commands
on the command line.
You can type the name of a command with multiple short options. The output of all of
these examples is the same, -l will give a long listing, while -r reverses the display order
of the results:
ls -l -r
ls -rl
ls -lr
sysadmin@localhost:~$ ls -l -r
total 0
drwxr-xr-x 1 sysadmin sysadmin 420 Sep 18 22:25 test
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Videos
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Templates
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Public
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Pictures
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Music
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Downloads
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Documents
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Desktop
Generally, short options can be combined with other short options in any order. The
exception to this is when an option requires an argument.
For example, the -w option to the ls command specifies the width of the output desired
and therefore requires an argument. If combined with other options, the -w option can
be specified last, followed by its argument and still be valid, as in ls -rtw 40, which
specifies an output width of 40 characters. Otherwise, the -w option cannot be combined
with other options, and must be given separately.
sysadmin@localhost:~$ ls -l -r
total 0
drwxr-xr-x 1 sysadmin sysadmin 420 Sep 18 22:25 test
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Videos
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Templates
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Public
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Pictures
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Music
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Downloads
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Documents
drwxr-xr-x 1 sysadmin sysadmin 0 Sep 18 22:25 Desktop
If using multiple options that require arguments, then don't combine them. For
example, the -T option also requires an argument. In order to accommodate both
arguments, each option is given separately:
sysadmin@localhost:~$ ls -w 40 -T 12
Desktop Music Templates
Documents Pictures Videos
Downloads Public test
Some commands support additional options that are longer than a single
character. Long options for commands are preceded by a double hyphen --and the
meaning of the option is typically the name of the option, like --all. For example:
sysadmin@localhost:~$ ls --all
. .bashrc .selected_editor Downloads Public test
.. .cache Desktop Music Templates
.bash_logout .profile Documents Pictures Videos
For commands that support both long and short options, execute the command using
the long and short options concurrently:
Commands that support long options will often also support arguments that may be
specified with or without an equal symbol (the output of both commands is the same):
ls --sort time
ls --sort=time
sysadmin@localhost:~$ ls --sort=time
Desktop Documents Downloads Music Pictures Public Templates Videos test
A special option exists, the "lone" double hyphen --, which can be used to indicate the
end of all options for the command. This can be useful in some circumstances where it is
unclear whether some text that follows the options should be interpreted as an
additional option or as an argument to the command.
For example, if the touch command tries to create a file called --badname:
sysadmin@localhost:~$ touch --badname
touch: unrecognized option '--badname'
Try 'touch --help' for more information.
Note: The file name in the previous example is considered to be "bad" because putting
hyphens in the beginning of file names, while allowed, can cause problems when trying
to access the file.
Consider This
A third type of option exists for a select few commands. While the options used in the
AT&T version of UNIX used a single hyphen and the GNU port of those commands used
two hyphens, the Berkley Software Distribution (BSD) version of UNIX used options
with no hyphen at all.
This "no hyphen" syntax is fairly rare in most Linux distributions. A couple of notable
commands that support the BSD UNIX style options are the ps and tar commands; both of
these commands also support the single and double hyphen style of options.
In the terminal below, there are two similar commands, the first command is executed
with a traditional UNIX style option (with single hyphens) and the second command is
executed with a BSD style option (no hyphens).
sysadmin@localhost:~$ ps -u sysadmin
PID TTY TIME CMD
79 ? 00:00:00 bash
122 ? 00:00:00 ps
sysadmin@localhost:~$ ps u
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
sysadmin 79 0.0 0.0 18176 3428 ? S 20:23 0:00 -bash
sysadmin 120 0.0 0.0 15568 2040 ? R+ 21:26 0:00 ps u
Using no quotes failed because the shell interprets the !! characters as special shell
characters; in this case they mean "replace the !! with the last command that was
executed". In this case, the last command was the cd command, so cd replaced !! and then
the echo command displayed hello worldcd to the screen.
You may want to try the double quotes to see if they will block the interpretation
(or expansion) of the !! characters. The double quotes block the expansion of some
special characters, but not all of them. Unfortunately, double quotes do not block the
expansion of !!:
sysadmin@localhost:~$ cd
sysadmin@localhost:~$ echo "hello world!!"
echo "hello worldcd"
hello worldcd
Using double quotes preserves the literal value of all characters that they enclose except
the $ (dollar sign), ` (backquote), \ (backslash) and !(exclamation point).
When you enclose text within the ' (single quote) characters, then all characters have
their literal meaning:
sysadmin@localhost:~$ cd
sysadmin@localhost:~$ echo 'hello world!!'
hello world!!
sysadmin@localhost:~$ uname
Linux
The uname command is useful for several reasons, including when you need to determine
the name of the computer as well as the current version of the kernel that is being used.
sysadmin@localhost:~$ pwd
/home/sysadmin
sysadmin@localhost:~$ ca
cal capsh cat cautious-launcher
calendar captoinfo catchsegv
caller case catman
sysadmin@localhost:~$ ca
Another possibility may occur when you have typed too little to match a single
command name uniquely. If there are more possible matches to what you've typed than
can easily be displayed, then the system will ask you if you want to display all
possibilities.
For example, if you just type c and press the Tab key twice, the system may provide you
with a prompt like:
Chapter Objectives
Chapter 1: Using the Shell
This chapter will cover the following exam objectives:
103.1: Work on the command line
Weight: 4
Description: Candidates should be able to interact with shells and commands using the
command line. The objective assumes the Bash shell.
Key Knowledge Areas:
Use single shell commands and one line command sequences to perform basic tasks
on the command line
Section 1.7 | Section 1.9 | Section 1.10 | Section 1.11
KEY TERMS
echo
ls
Command that will list information about files. The current directory is listed by default.
Section 1.9
pwd
uname
Print certain system information such as kernel name, network node hostname, kernel
release, kernel version, machine hardware name, processor type, hardware platform,
and operating system, depending on options provided.
Section 1.13