Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
The terminal accepts what the user types and passes to a shell. The shell interprets what the user
has typed into instructions that can be executed by the operating system. If output is produced by the
command, then this text is displayed in the terminal. If problems with the command are encountered,
then an error message is displayed.
There are many ways to access a terminal window. Some systems will boot directly to a terminal.
This is often the case with servers, as a Graphical User Interface (GUI) can be resource intensive
and may not be needed to perform server-based operations.
A good example of a server that doesn't necessarily require a GUI is a web server. Web servers
need to run as quickly as possible and a GUI would just slow the system down.
On systems that boot to a GUI, there are commonly two ways to access a terminal, a GUI-based
terminal and a virtual terminal:
A GUI terminal is a program within the GUI environment that emulates a terminal window.
GUI terminals can be accessed through the menu system. For example, on a CentOS
machine, you could click on Applications on the menu bar, then System Tools > and,
finally, Terminal:
A virtual terminal can be run at the same time as a GUI, but requires the user to log in via the
virtual terminal before they can execute commands (as they would before accessing the GUI
interface). Most systems have multiple virtual terminals that can be accessed by pressing a
combination of keys, for example: Ctrl-Alt-F1
4.3.1 Prompt
A terminal window displays a prompt; the prompt appears when no commands are being run and
when all command output has been printed to the screen. The prompt is designed to tell the user to
enter a command.
The structure of the prompt may vary between distributions, but will typically contain information
about the user and the system. Below is a common prompt structure:
sysadmin@localhost:~$
The previous prompt provides the name of the user that is logged in (sysadmin), the name of the
system (localhost) and the current directory (~). The ~ symbol is used as shorthand for the user's
home directory (typically the home directory for the user is under the /home directory and named
after the user account name, for example: /home/sysadmin).
4.3.2 Shell
A shell is the interpreter that translates commands entered by a user into actions to be performed by
the operating system. The Linux environment provides many different types of shells, some of which
have been around for many years.
The most commonly used shell for Linux distributions is called the BASH shell. It is a shell that
provides many advanced features, such as command history, which allows you to easily re-execute
previously executed commands.
Scripting: The ability to place commands in a file and execute the file, resulting in all of the
commands being executed. This feature also has some programming features, such as
conditional statements and the ability to create functions (AKA, subroutines).
Variables: Variables are used to store information for the BASH shell. These variables can
be used to modify how commands and features work as well as provide vital system
information.
Note: The previous list is just a short summary of some of the many features provided by the BASH
shell.
Options are used to modify the core behavior of a command while arguments are used to provide
additional information (such as a filename or a username). Each option and argument is normally
separated by a space, although options can often be combined together.
Keep in mind that Linux is case sensitive. Commands, options, arguments, variables and filenames
must be entered exactly as shown.
The ls command will provide useful examples. By itself, the ls command will list the files and
directories contained in your current working directory:
sysadmin@localhost:~$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
sysadmin@localhost:~$
The ls command will be covered in complete detail in a later chapter. The purpose of introducing this
command now is to demonstrate how arguments and options work. At this point you shouldn't worry
about what the output of the command is, but rather focus on understanding what an argument and
an option is.
An argument can also be passed to the ls command to specify which directory to list the contents of.
For example, the command ls /etc/ppp will list the contents of the /etc/ppp directory instead of
the current directory:
sysadmin@localhost:~$ ls /etc/ppp
ip-down.d ip-up.d
sysadmin@localhost:~$
Since the ls command will accept multiple arguments, you can list the contents of multiple
directories at once by typing the ls /etc/ppp /etc/sshcommand:
For example, you can use the -l option with the ls command to display more information about the
files that are listed. The ls -l command will list the files contained within the current directory and
provide additional information, such as the permissions, the size of the file and other information:
sysadmin@localhost:~$ ls -l
total 0
drwxr-xr-x 1 sysadmin sysadmin 0 Jan 29 2015 Desktop
drwxr-xr-x 1 sysadmin sysadmin 0 Jan 29 2015 Documents
drwxr-xr-x 1 sysadmin sysadmin 0 Jan 29 2015 Downloads
drwxr-xr-x 1 sysadmin sysadmin 0 Jan 29 2015 Music
drwxr-xr-x 1 sysadmin sysadmin 0 Jan 29 2015 Pictures
drwxr-xr-x 1 sysadmin sysadmin 0 Jan 29 2015 Public
drwxr-xr-x 1 sysadmin sysadmin 0 Jan 29 2015 Templates
drwxr-xr-x 1 sysadmin sysadmin 0 Jan 29 2015 Videos
sysadmin@localhost:~$
In most cases, options can be used in conjunction with other options. For example, the ls -l
-h or ls -lh command will list files with details, but will display the file sizes in human-
readable format instead of the default value (bytes):
sysadmin@localhost:~$ ls -l /usr/bin/perl
-rwxr-xr-x 2 root root 10376 Feb 4 2014 /usr/bin/perl
sysadmin@localhost:~$ ls -lh /usr/bin/perl
-rwxr-xr-x 2 root root 11K Feb 4 2014 /usr/bin/perl
sysadmin@localhost:~$
Note that the previous example also demonstrated how you can combine single letter options: -lh .
The order of the combined options isn't important.
Options can often be used with an argument. In fact, some options require their own arguments. You
can use options and arguments with the ls command to list the contents of another directory by
executing the ls -l /etc/ppp command:
sysadmin@localhost:~$ ls -l /etc/ppp
total 0
drwxr-xr-x 1 root root 10 Jan 29 2015 ip-down.d
drwxr-xr-x 1 root root 10 Jan 29 2015 ip-up.d
sysadmin@localhost:~$
sysadmin@localhost:~$ date
Sun Nov 1 00:40:28 UTC 2015
sysadmin@localhost:~$ ls
Desktop Documents Downloads Music Pictures Public Templates
Videos
sysadmin@localhost:~$ cal 5 2015
May 2015
Su Mo Tu We Th Fr Sa
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
sysadmin@localhost:~$ history
1 date
2 ls
3 cal 5 2015
4 history
sysadmin@localhost:~$
Pressing the Up Arrow key will display the previous command on your prompt line. You can press
up repeatedly to move back through the history of commands you have run. Pressing the Enter key
will run the displayed command again.
When you find the command that you want to execute, you can use the Left arrow keys
and Right arrow keys to position the cursor for editing. Other useful keys for editing include
the Home, End, Backspace and Delete keys.
If you see a command you wish to run in the list that the history command generates, you can
execute this command by typing an exclamation point and then the number next to the command, for
example:
!3
sysadmin@localhost:~$ history
1 date
2 ls
3 cal 5 2015
4 history
sysadmin@localhost:~$ !3
cal 5 2015
May 2015
Su Mo Tu We Th Fr Sa
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
sysadmin@localhost:~$
Example Meaning
history 5 Show the last five commands from the history list
!-5 Execute the fifth command from the bottom of the history list
Variables are given names and stored temporarily in memory. When you close a terminal window or
shell, all of the variables are lost. However, the system automatically recreates many of these
variables when a new shell is opened.
To display the value of a variable, you can use the echo command. The echocommand is used to
display output in the terminal; in the example below, the command will display the value of
the HISTSIZEvariable:
sysadmin@localhost:~$ echo $HISTSIZE
1000
sysadmin@localhost:~$
The HISTSIZE variable defines how many previous commands to store in the history list. To display
the value of the variable, use a dollar sign$ character before the variable name. To modify the value
of the variable, you don't use the $ character:
sysadmin@localhost:~$ HISTSIZE=500
sysadmin@localhost:~$ echo $HISTSIZE
500
sysadmin@localhost:~$
There are many shell variables that are available for the BASH shell, as well as variables that will
affect different Linux commands. A discussion of all shell variables is beyond the scope of this
chapter, however more shell variables will be covered as this course progresses.
The term path refers to a list that defines which directories the shell will look in for commands. If you
type in a command and receive a "command not found" error, it is because the BASH shell was
unable to locate a command by that name in any of the directories included in the path. The following
command displays the path of the current shell:
Based on the proceeding output, when you attempt to execute a command, the shell will first look for
the command in the /usr/lib/qt-3.3/bin directory. If the command is found in that directory,
then it is executed. If it isn't found, then the shell will look in the /usr/local/bin directory.
If the command is not found in any directory listed in the PATH variable, then you will receive
a command not found error:
sysadmin@localhost:~$ zed
-bash: zed: command not found
sysadmin@localhost:~$
If custom software is installed on your system, you may need to modify the PATHto make it easier to
execute these commands. For example, the following will add the /usr/bin/custom directory to
the PATHvariable:
sysadmin@localhost:~$ PATH=/usr/bin/custom:$PATH
sysadmin@localhost:~$ echo $PATH
/
usr/bin/custom:/home/sysadmin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/us
r/bin:/sbin:/bin:/usr/games
sysadmin@localhost:~$
sysadmin@localhost:~$ variable1='Something'
To view environment variables, use the env command (searching through the output using grep, as
shown here, will be discussed in later chapters). In this case, the search for variable1 in the
environment variables results in no output:
After exporting variable1, it is now an environment variable. Notice that this time, it is found in the
search through the environment variables:
The export command can also be used to make an environment variable upon its creation:
sysadmin@localhost:~$ export variable2='Else'
sysadmin@localhost:~$ env | grep variable2
variable2=Else
To change the value of an environment variable, simply omit the $ when referencing it:
It would be tedious to have to manually look in each directory that is listed in the PATH variable.
Instead, you can use the which command to display the full path to the command in question:
The which command searches for the location of a command by searching the PATH variable.
The type command can also identify commands that are built into the bash (or other) shell:
In this case, the output is significantly different from the output of the whichcommand:
Using the -a option, the type command can also reveal the path of another command:
sysadmin@localhost:~$ type ll
ll is aliased to `ls -alF'
sysadmin@localhost:~$ type ls
ls is aliased to `ls --color=auto'
The output of these commands indicate that ll is an alias for ls -alF, and even ls is an alias for ls
--color=auto. Again, the output is significantly different from the which command:
sysadmin@localhost:~$ which ll
sysadmin@localhost:~$ which ls
/bin/ls
The type command supports other options, and can lookup multiple commands simultaneously. To
display only a single word describing the echo, ll, and which commands, use the -t option:
4.10 Aliases
An alias can be used to map longer commands to shorter key sequences. When the shell sees an
alias being executed, it substitutes the longer sequence before proceeding to interpret commands.
For example, the command ls -l is commonly aliased to l or ll. Because these smaller
commands are easier to type, it becomes faster to run the ls -lcommand line.
You can determine what aliases are set on your shell with the alias command:
sysadmin@localhost:~$ alias
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
The aliases that you see from the previous examples were created by initialization files. These files
are designed to make the process of creating aliases automatic and will be discussed in more detail
in a later chapter.
New aliases can be created by typing alias name=command where name is the name you want to
give the alias and command is the command you want to have executed when you run the alias.
For example, you can create an alias so that lh displays a long listing of files, sorted by size with a
"human friendly" size with the alias lh='ls -Shl'command. Typing lh should now result in the
same output as typing the ls -Shl command:
4.11 Globbing
Glob characters are often referred to as "wild cards". These are symbols that have special meaning
to the shell.
Unlike commands that the shell will run, or options and arguments that the shell will pass to
commands, glob characters are interpreted by the shell itself before it attempts to run any command.
This means that glob characters can be used with any command.
Globs are powerful because they allow you to specify patterns that match filenames in a directory, so
instead of manipulating a single file at a time, you can easily execute commands that will affect many
files. For instance, by using glob characters it is possible to manipulate all files with a certain
extension or with a particular filename length.
Keep in mind that these globs can be used with any command because it is the shell, not the
command that expands with globs into matching filenames. The examples provided in this chapter
will use the echo command for demonstration.
The pattern t* means "match any file that begins with the character t and has zero or more of any
character after the t".
You can use the asterisk character at any place within the filename pattern. For example, the
following will match any filename in the /etc directory that ends with .d:
In the next example, all of the files in the /etc directory that begin with the letter r and end
with .conf will be displayed:
sysadmin@localhost:~$ echo /etc/r*.conf
/etc/resolv.conf /etc/rsyslog.conf
Suppose you want to display all of the files in the /etc directory that begin with the letter t and
have exactly 7 characters after the t character:
Glob characters can be used together to find even more complex patterns. The echo
/etc/*???????????????????? command will print only files in the /etc directory with twenty or
more characters in the filename:
The asterisk and question mark could also be used together to look for files with three-letter
extensions by running the echo /etc/*.??? command:
4.11.3 Brackets []
Brackets are used to match a single character by representing a range of characters that are
possible match characters. For example, echo /etc/[gu]*will print any file that begins with either
a g or u character and contains zero or more additional characters:
The echo /etc/*[0-9]* command would display any file that contains at least one number:
The range is based on the ASCII text table. This table defines a list of characters, arranging them in
a specific standard order. If you provide an invalid order, no match will be made:
4.12 Quoting
There are three types of quotes that have special significance to the Bash shell: double quotes ",
single quotes ', and back quotes `. Each set of quotes indicates to the shell that it should treat the
text within the quotes differently than it would normally be treated.
This is useful when you want to display something on the screen that is normally a special character
to the shell:
Double quotes still allow for command substitution (discussed later in this chapter), variable
substitution and permit some other shell metacharacters that haven't been discussed yet. For
example, in the following demonstration, you will notice that the value of the PATH variable is
displayed:
For example, if you want the $ character to simply mean a $, rather than it acting as an indicator to
the shell to look for the value of a variable, you could execute the second command displayed below:
If you place a backslash \ character in front of another character, it treats the other character as a
"single quoted" character. The third command below demonstrates using the \ character while the
other two demonstrate how the variables would be treated within double and single quotes:
sysadmin@localhost:~$ echo "The service costs $100 and the path is $PATH"
The service costs 00 and the path is
/usr/bin/custom:/home/sysadmin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/u
sr/bin:/sbin:/bin:/usr/games
sysadmin@localhost:~$ echo 'The service costs $100 and the path is $PATH'
The service costs $100 and the path is $PATH
sysadmin@localhost:~$ echo The service costs \$100 and the path is $PATH
The service costs $100 and the path is
/usr/bin/custom:/home/sysadmin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/u
sr/bin:/sbin:/bin:/usr/games
sysadmin@localhost:~$
While it may sound confusing, an example should make things more clear. To begin, note the output
of the date command:
sysadmin@localhost:~$ date
Mon Nov 2 03:35:50 UTC 2015
Now note the output of the echo Today is date command line:
4.13.1 Semicolon
The semicolon can be used to run multiple commands, one after the other. Each command runs
independently and consecutively; no matter the result of the first command, the second will run once
the first has completed, then the third and so on.
For example, if you want to print the months of January, February and March of 2015, you can
execute cal 1 2015; cal 2 2015; cal 3 2015 on the command line:
February 2015
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
March 2015
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
To better understand how this works, consider first the concept of failure and success for commands.
Commands succeed when they work properly and fail when something goes wrong. For example,
consider the ls /etc/xmlcommand line. The command will succeed if the /etc/xml directory is
accessible and fail if it isn't.
For example, the first command will succeed because the /etc/xml directory exists and is
accessible while the second command will fail because there is no /junk directory:
sysadmin@localhost:~$ ls /etc/xml
catalog catalog.old xml-core.xml xml-core.xml.old
sysadmin@localhost:~$ ls /etc/junk
ls: cannot access /etc/junk: No such file or directory
sysadmin@localhost:~$
The way that you would use the success or failure of the ls command in conjunction with && would
be to execute a command line like the following:
With the double pipe, if the first command runs successfully, the second command is skipped; if the
first command fails, then the second command will be run. In other words, you are essentially telling
the shell, "Either run this first command or the second one.
In the following example, the echo command will only execute if the lscommand fails:
4.2.1 Step 1
The following command will display the same information that you see in the first part of the prompt.
Make sure that you have selected (clicked on) the Terminal window first and then type the following
command followed by the Enter key:
whoami
The output of the whoami command, sysadmin, displays the user name of the current user.
Although in this case your username is displayed in the prompt, this command could be used to
obtain this information in a situation when the prompt did not contain this information.
4.2.2 Step 2
The next command displays information about the current system. To be able to see the name of the
kernel you are using, type the following command into the terminal:
uname
sysadmin@localhost:~$ uname
Linux
Many commands that are executed produce text output like this. You can change what output is
produced by a command by using options after the name of the command.
Options for a command can be specified in several ways. Traditionally in UNIX, options were
expressed by a hyphen followed by another character, for example: -n.
In Linux, options can sometimes also be given by two hyphen characters followed by a word, or
hyphenated word, for example: --nodename.
Execute the name command again twice in the terminal, once with the option -nand again with the
option --nodename. This will display the network node hostname, also found in the prompt.
uname -n
uname --nodename
4.2.3 Step 3
The pwd command is used to display your current "location" or current "working" directory. Type the
following command to display the working directory:
pwd
sysadmin@localhost:~$ pwd
/home/sysadmin
sysadmin@localhost:~$
The current directory in the example above is /home/sysadmin. This is also referred to as
your home directory, a special place where you have control of files and other users normally have
no access. By default, this directory is named the same as your username and is located underneath
the /home directory.
As you can see from the output of the command, /home/sysadmin, Linux uses the forward
slash / to separate directories to make what is called a path. The initial forward slash represents the
top level directory, known as the root directory. More information regarding files, directories and
paths will be presented in later labs.
The tilde ~ character that you see in your prompt is also indicating what the current directory is. This
character is a "shortcut" way to represent your home.
Consider This: pwd stands for "print working directory". While it doesn't actually "print" in modern
versions, older UNIX machines didn't have monitors and output of commands went to a printer,
hence the funny name of pwd.
4.3.1 Step 1
The echo command can be used to print text, the value of a variable and show how the shell
environment expands metacharacters (more on metacharacters later in this lab). Type the following
command to have it output literal text:
4.3.2 Step 2
Type the following command to display the value of the PATH variable:
echo $PATH
The PATH variable is displayed by placing a $ character in front of the name of the variable.
This variable is used to find the location of commands. Each of the directories listed above are
searched when you run a command. For example, if you try to run the date command, the shell will
first look for the command in the /home/sysadmin/bin directory and then in
the /usr/local/sbin directory and so on. Once the date command is found, the shell "runs it.
4.3.3 Step 3
Use the which command to determine if there is an executable file named date that is located in a
directory listed in the PATH value:
which date
The output of the which command tells you that when you execute the datecommand, the system
will run the command /bin/date. The which command makes use of the PATH variable to
determine the location of the datecommand.
4.4 Globbing
The use of glob characters in Linux is similar to what many operating systems refer to as "wildcard"
characters. Using glob characters, you match filenames using patterns.
Glob characters are a shell feature, not something that is particular to any specific command. As a
result, you can use glob characters with any Linux command.
When glob characters are used, the shell will "expand" the entire pattern to match all files in the
specified directory that matches the pattern.
For demonstration purposes, we will use the echo command to display this expansion process.
4.4.1 Step 1
Use the following echo command to display all filenames in the current directory that matches the
glob pattern *:
echo *
The asterisk * matches "zero or more" characters in a file name. In the example above, this results
in matching all filenames in the current directory.
The echo command, in turn, displays the filenames that were matched.
4.4.2 Step 2
The following commands will display all the files in the current directory that start with the letter D,
and the letter P:
echo D*
echo P*
sysadmin@localhost:~$ echo D*
Desktop Documents Downloads
sysadmin@localhost:~$ echo P*
Pictures Public
sysadmin@localhost:~$
Think of the first example, D*, as "match all filenames in the current directory that begin with a
capital d character and have zero or more of any other character after the D".
4.4.3 Step 3
The asterisk * can be used anywhere in the string. The following command will display all the files in
your current directory that end in the letter s:
echo *s
Your output should be similar to the following:
sysadmin@localhost:~$ echo *s
Documents Downloads Pictures Templates Videos
sysadmin@localhost:~$
4.4.4 Step 4
Notice that the asterisk can also appear multiple times or in the middle of several characters:
echo D*n*s
The next glob metacharacter that we will examine is the question mark ?. The question mark
matches exactly one character. This single character can be any possible character.
Like the asterisk it can be used anywhere in a string and can appear multiple times.
4.4.5 Step 5
Since each question mark matches one unknown character, typing six of them will match six
character filenames. Type the following to display the filenames that are exactly six characters long:
echo ??????
4.4.6 Step 6
Using the question mark with other characters will limit the matches. Type the following to display the
file names that start with the letter D and are exactly nine characters long:
echo D????????
4.4.7 Step 7
Wildcards or glob characters can be combined together. The following command will display file
names that are at least six characters long and end in the letter s.
echo ?????*s
Think of the pattern ?????*s to mean "match filenames that begin with any five characters, then
have zero or more of any characters and then end with an scharacter".
4.4.8 Step 8
The next glob is similar to the question mark glob to specify one character.
This glob uses a pair of square brackets [ ] to specify which one character will be allowed. The
allowed characters can be specified as a range, a list, or by what is known as a character class.
In the first example, the first character of the file name can be either a D or a P. In the second
example, the first character can be any character except a D or P:
echo [DP]*
echo [!DP]*
4.4.9 Step 9
In these next examples, a range of characters will be specified. In the first example, the first
character of the file name can be any character starting at Dand ending at P. In the second example,
this range of characters is negated, meaning any single character will match as long as it is not
between the letters Dand P:
echo [D-P]*
echo [!D-P]*
You may be asking yourself "who decides what letters come between D and P?". In this case the
answer is fairly obvious (E, F, G, H, I, J, K, L, M, N and O), but what if the range was [1-A]?
The ASCII text table is used to determine the range of characters. You can view this table by
searching for it on the Internet or typing the following command: ascii
So, what characters does the glob [1-A] match? According to the ASCII text
table: 1, 2, 3, 4, 5, 6, 7, 8, 9, :, ;, <, =, >, ?, @ and A.
4.5 Quoting
There are three types of quotes used by the Bash shell: single quotes ('), double quotes (") and
back quotes (`). These quotes have special features in the Bash shell as described below.
To understand single and double quotes, consider that there are times that you don't want the shell
to treat some characters as "special". For example, as you learned earlier in this lab, the * character
is used as a wildcard. What if you wanted the * character to just mean an asterisk?
Single quotes prevent the shell from "interpreting" or expanding all special characters. Often single
quotes are used to protect a string from being changed by the shell, so that the string can be
interpreted by a command as a parameter to affect the way the command is executed.
Double quotes stop the expansion of glob characters like the asterisk (*), question mark (?), and
square brackets ( [ ] ). Double quotes do allow for both variable expansion and command
substitution (see back quotes) to take place.
Back quotes cause "command substitution" which allows for a command to be executed within the
line of another command.
When using quotes, they must be entered in pairs or else the shell will not consider the command
complete.
While single quotes are useful for blocking the shell from interpreting one or more characters, the
shell also provides a way to block the interpretation of just a single character called "escaping" the
character. To "escape" the special meaning of a shell metacharacter, the backslash \ character is
used as a prefix to that one character.
4.5.1 Step 1
Execute the following command to use back quotes ` to execute the datecommand within the line of
the echo command:
4.5.2 Step 2
You can also place $( before the command and ) after the command to accomplish command
substitution:
Why two different methods that accomplish the same thing? Backquotes look very similar to single
quotes, making it harder to "see" what a command is supposed to do. Originally shells used
backquotes; the $(command) format was added in a later version of the Bash shell to make the
statement more visually clear.
4.5.3 Step 3
If you don't want the backquotes to be used to execute a command, place single quotes around
them. Execute the following:
4.5.4 Step 4
Note that you could also place a backslash character in front of each backquote character. Execute
the following:
4.5.5 Step 5
Double quote characters don't have any effect on backquote characters. The shell will still use them
as command substitution. Execute the following to see a demonstration:
4.5.6 Step 6
Double quote characters will have an effect on wildcard characters, disabling their special meaning.
Execute the following:
echo D*
echo "D*"
sysadmin@localhost:~$ echo D*
Desktop Documents Downloads
sysadmin@localhost:~$ echo "D*"
D*
sysadmin@localhost:~$
Important:
Quoting may seem trivial and weird at the moment, but as you gain more experience working in the
command shell, you will discover that having a good understanding of how different quotes work is
critical to using the shell.
The simplest separator is the semicolon (;). Using the semicolon between multiple commands
allows for them to be executed one right after another, sequentially from left to right.
The && characters create a logical and statement. Commands separated by &&are conditionally
executed. If the command on the left of the && is successful, then the command to the right of
the && will also be executed. If the command to the left of the && fails, then the command to the right
of the && is not executed.
The || characters create a logical or statement, which also causes conditional execution. When
commands are separated by ||, then only if the command to the left fails, does the command to the
right of the || execute. If the command to the left of the || succeeds, then the command to the right
of the || will not execute.
To see how these control statements work, you will be using two special
executables: true and false. The true executable always succeeds when it executes, whereas,
the false executable always fails. While this may not provide you with realistic examples of
how && and || work, it does provide a means to demonstrate how they work without having to
introduce new commands.
4.6.1 Step 1
Execute the following three commands together separated by semicolons:
As you can see the output shows all three commands executed sequentially:
4.6.2 Step 2
Now, put three commands together separated by semicolons, where the first command executes
with a failure result:
Note that in the previous example, all three commands still executed even though the first one failed.
While you can't see from the output of the false command, it did execute. However, when
commands are separated by the ; character, they are completely independent of each other.
4.6.3 Step 3
Next, use "logical and" to separate the commands:
Because each echo statement executes correctly, a return value of success is provided, allowing the
next statement to also be executed.
4.6.4 Step 4
Use "logical and" with a command that fails as shown below:
echo Success && false && echo Bye
The first echo command succeeds and we see its output. The false command executes with a
failure result, so the last echo statement is not executed.
4.6.5 Step 5
The "or" characters separating the following commands demonstrates how the failure before the "or"
statement causes the command after it to execute; however, a successful first statement causes the
command to not execute:
The first and easiest way to recall a previous command is to use the up arrow key. Each press of
the up arrow key goes backwards one command through history. If you accidentally go back too far,
then the down arrow key will go forwards through the history of commands.
When you find the command that you want to execute, you can use the left arrow keys and right
arrow keys to position the cursor for editing. Other useful keys for editing include
the Home, End, Backspace and Delete keys.
Another way to use your command history is to execute the history command to be able to view
a numbered history list. The number listed to the left of the command can be used to execute the
command again. The history command also has a number of options and arguments which can
manipulate which commands will be stored or displayed.
4.7.1 Step 1
Execute a few commands and then execute the history command:
date
clear
echo Hi
history
Remember: The date command will print the time and date on the system. The clear command
clears the screen.
sysadmin@localhost:~$ echo Hi
Hi
sysadmin@localhost:~$ history
1 date
2 clear
3 echo Hi
4 history
sysadmin@localhost:~$
Your command numbers will likely differ from those provided above. This is because you likely have
executed a different number of commands.
4.7.2 Step 2
To view a limited number of commands, the history command can take a number as a parameter
to display exactly that many recent entries. Type the following command to display the last five
commands from your history:
history 5
sysadmin@localhost:~$ history 5
185 false || Fail Or
186 false || echo Fail Or
187 true || echo Nothing to see here
188 history
189 history 5
sysadmin@localhost:~$
4.7.3 Step 3
To execute a command again, type the exclamation point and the history list number. For example, to
execute the 94th command in your history list, you would execute the following:
!94
4.7.4 Step 4
Next, experiment with accessing your history using the up arrow keys and down arrow keys. Keep
pressing the up arrow key until you find a command you want to execute. If necessary use other
keys to edit the command and then press Enter to execute the command.