Modul 4 – Command Line Skills
Source: https://content.netdevgroup.com/contents/linux-essentials/4/
4.1 Introduction
If you are like most people, you are probably most familiar with using a Graphical User Interface (GUI) to control your computer. Introduced to the masses by Apple on the Macintosh computer and popularized by Microsoft, a GUI provides an easy, discoverable way to manage your system. Without a GUI, some tools for graphics and video would not be practical.
Prior to the popularity of the GUI, the Command Line Interface (CLI) was the preferred way to control a computer. The CLI relies solely on keyboard input. Everything you want the computer to do is relayed by typing commands rather than clicking on icons.
If you have never used a CLI, at first it may prove challenging because it requires memorizing commands and their options. However, a CLI provides more precise control, greater speed and the ability to easily automate tasks through scripting (see sidebar). Although Linux does have many GUI environments, you will be able to control Linux much more effectively by using the Command Line Interface.
![Why is knowing the command line important? Flexibility and mobility!! By understanding the foundation of Linux, you have the ability to work on ANY Linux distribution. This could mean one company with a mixed environment or a new company with a different Linux distribution.](https://ndg-content-dev.s3.amazonaws.com/media/images/4-LPI-Graphics.png)
4.2 Command Line Interface (CLI)
The Command Line Interface (CLI), is a text-based interface to the computer, where the user types in a command and the computer then executes it. The CLI environment is provided by an application on the computer known as a terminal.
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.
4.3 Accessing a Terminal
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
Note: On virtual machines, virtual terminals may not be available.
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.
The BASH shell also has other popular features:
- 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).
- Aliases: The ability to create short “nicknames” for longer commands.
- 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.
4.3.3 Formatting commands
Many commands can be used by themselves with no further input. Some commands require additional input to run properly. This additional input comes in two forms: options and arguments.
The typical format for a command is as follows:
command [options] [arguments]
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/ssh
command:
sysadmin@localhost:~$ ls /etc/ppp /etc/ssh /etc/ppp: ip-down.d ip-up.d /etc/ssh: moduli ssh_host_dsa_key.pub ssh_host_rsa_key sshd_configssh_config ssh_host_ecdsa_key ssh_host_rsa_key.pub ssh_host_dsa_key ssh_host_ecdsa_key.pub ssh_import_id sysadmin@localhost:~$
4.3.4 Working with Options
Options can be used with commands to expand or modify the way a
command behaves. Options are often single letters; however, they
sometimes will be “words” as well. Typically, older commands use single
letters while newer commands use complete words for options.
Single-letter options are preceded by a single dash -
. Full-word options are preceded by two dashes --
.
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.
The -h
option also has a full-word form: --human-readable
.
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:~$
4.4 Command history
When you execute a command in a terminal, the command is stored in a “history list”. This is designed to make it easy for you to execute the same command later since you won’t need to retype the entire command.
To view the history list of a terminal, use the history
command:
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:~$
Some additional history
examples:
Example | Meaning |
---|---|
history 5 | Show the last five commands from the history list |
!! | Execute the last command again |
!-5 | Execute the fifth command from the bottom of the history list |
!ls | Execute the most recent ls command |
4.5 Introducing BASH shell variables
A BASH shell variable is a feature that allows you or the shell to store data. This data can be used to provide critical system information or to change the behavior of how the BASH shell (or other commands) work.
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 echo
command is used to display output in the terminal; in the example below, the command will display the value of the HISTSIZE
variable:
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.
4.6 PATH variable
One of the most important BASH shell variables to understand is the PATH
variable.
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:
sysadmin@localhost:~$ echo $PATH /home/sysadmin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin: /usr/games sysadmin@localhost:~$
Based on the proceeding output, when you attempt to execute a command, the shell will first look for the command in the /home/sysadmin/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/sbin
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 PATH
to make it easier to execute these commands. For example, the following will add the /usr/bin/custom
directory to the PATH
variable:
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:/usr/bin:/sbin:/bin:/usr/games sysadmin@localhost:~$
4.7 export Command
There are two types of variables used in the BASH shell, local and environment. Environment variables, such as PATH
and HOME
,
are used by BASH when interpreting commands and performing tasks. Local
variables are often associated with user based tasks and are lowercase
by convention. To create a local variable, simply type:
sysadmin@localhost:~$ variable1='Something'
To view the contents of the variable, refer to it with a leading $
sign:
sysadmin@localhost:~$ echo $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:
sysadmin@localhost:~$ env | grep variable1 sysadmin@localhost:~$
After exporting variable1
, it is now an environment variable. Notice that this time, it is found in the search through the environment variables:
sysadmin@localhost:~$ export variable1 sysadmin@localhost:~$ env | grep variable1 variable1=Something
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:
sysadmin@localhost:~$ variable1=$variable1' '$variable2 sysadmin@localhost:~$ echo $variable1 Something Else
Exported variables can be removed using the unset
command:
sysadmin@localhost:~$ unset variable2
4.8 which Command
There may be situations where different versions of the same command are installed on a system or where commands are accessible to some users and not others. If a command does not behave as expected or if a command is not accessible that should be, it can be beneficial to know where the shell is finding the command or which version it is using.
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:
sysadmin@localhost:~$ which date /bin/date sysadmin@localhost:~$ which cal /usr/bin/cal sysadmin@localhost:~$
The which
command searches for the location of a command by searching the PATH
variable.
4.9 type Command
The type
command can be used to determine information about various commands. Some commands originate from a specific file:
sysadmin@localhost:~$ type which which is hashed (/usr/bin/which)
This output would be similar to the output of the which
command (as discussed in the previous section, which displays the full path of the command):
sysadmin@localhost:~$ which which /usr/bin/which
The type
command can also identify commands that are built into the bash (or other) shell:
sysadmin@localhost:~$ type echo echo is a shell builtin
In this case, the output is significantly different from the output of the which
command:
sysadmin@localhost:~$ which echo /bin/echo
Using the -a
option, the type
command can also reveal the path of another command:
sysadmin@localhost:~$ type -a echo echo is a shell builtin echo is /bin/echo
The type
command can also identify aliases to other commands:
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:
sysadmin@localhost:~$ type -t echo ll which builtin alias file
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 -l
command 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:
sysadmin@localhost:~$ alias lh='ls -Shl' sysadmin@localhost:~$ lh /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
Aliases created this way will only persist while the shell is open. Once the shell is closed, the new aliases that you created will be lost. Additionally, each shell has its own aliases, so if you create an alias in one shell and then open another shell, you won’t see the alias in the new shell.
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.
4.11.1 Asterisk (*)
The asterisk character is used to represent zero or more of any
character in a filename. For example, suppose you want to display all of
the files in the /etc
directory that begin with the letter t
:
sysadmin@localhost:~$ echo /etc/t* /etc/terminfo /etc/timezone sysadmin@localhost:~$
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
:
sysadmin@localhost:~$ echo /etc/*.d /etc/apparmor.d /etc/bash_completion.d /etc/cron.d /etc/depmod.d /etc/fstab.d /etc/init.d /etc/insserv.conf.d /etc/ld.so.conf.d /etc/logrotate.d /etc/modprobe.d /etc/pam.d /etc/profile.d /etc/rc0.d /etc/rc1.d /etc/rc2.d /etc/rc3.d /etc/rc4.d /etc/rc5.d /etc/rc6.d /etc/rcS.d /etc/rsyslog.d /etc/sudoers.d /etc/sysctl.d /etc/update-motd.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
4.11.2 Question Mark (?)
The question mark represents any one character. Each question mark character matches exactly one character, no more and no less.
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:
sysadmin@localhost:~$ echo /etc/t??????? /etc/terminfo /etc/timezone sysadmin@localhost:~$
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:
sysadmin@localhost:~$ echo /etc/*???????????????????? /etc/bindresvport.blacklist /etc/ca-certificates.conf sysadmin@localhost:~$
The asterisk and question mark could also be used together to look for files with three-letter extensions by running the echo /etc/*.???
command:
sysadmin@localhost:~$ echo /etc/*.??? /etc/blkid.tab /etc/issue.net sysadmin@localhost:~$
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:
sysadmin@localhost:~$ echo /etc/[gu]* /etc/gai.conf /etc/groff /etc/group /etc/group- /etc/gshadow /etc/gshadow- /etc/ucf.conf /etc/udev /etc/ufw /etc/update-motd.d /etc/updatedb.conf sysadmin@localhost:~$
Brackets can also be used to a represent a range of characters. For example, the echo /etc/[a-d]*
command will print all files that begin with any letter between and including a
and d
:
sysadmin@localhost:~$ echo /etc/[a-d]* /etc/adduser.conf /etc/adjtime /etc/alternatives /etc/apparmor.d /etc/apt /etc/bash.bashrc /etc/bash_completion.d /etc/bind /etc/bindresvport.blacklist /etc/blkid.conf /etc/blkid.tab /etc/ca-certificates /etc/ca-certificates.conf /etc/calendar /etc/cron.d /etc/cron.daily /etc/cron.hourly /etc/cron.monthly /etc/cron.weekly /etc/crontab /etc/dbus-1 /etc/debconf.conf /etc/debian_version /etc/default /etc/deluser.conf /etc/depmod.d /etc/dpkg sysadmin@localhost:~$
The echo /etc/*[0-9]*
command would display any file that contains at least one number:
sysadmin@localhost:~$ echo /etc/*[0-9]* /etc/dbus-1 /etc/iproute2 /etc/mke2fs.conf /etc/python2.7 /etc/rc0.d /etc/rc1.d /etc/rc2.d /etc/rc3.d /etc/rc4.d /etc/rc5.d /etc/rc6.d sysadmin@localhost:~$
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:
sysadmin@localhost:~$ echo /etc/*[9-0]* /etc/*[9-0]* sysadmin@localhost:~$
4.11.4 Exclamation Point (!)
The exclamation point is used in conjunction with the square brackets to negate a range. For example, the command echo [!DP]*
will display any file that does not begin with a D
or P
.
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.
4.12.1 Double Quotes
Double quotes will stop the shell from interpreting some
metacharacters, including glob characters. Within double quotes an
asterisk is just an asterisk, a question mark is just a question mark,
and so on. This means that when you use the second echo
command below, the BASH shell doesn’t convert the glob pattern into filenames that match the pattern:
sysadmin@localhost:~$ echo /etc/[DP]* /etc/DIR_COLORS /etc/DIR_COLORS.256color /etc/DIR_COLORS.lightbgcolor /etc/PackageKit sysadmin@localhost:~$ echo "/etc/[DP]*" /etc/[DP]* sysadmin@localhost:~$
This is useful when you want to display something on the screen that is normally a special character to the shell:
sysadmin@localhost:~$ echo "The glob characters are *, ? and [ ]" The glob characters are *, ? and [ ] sysadmin@localhost:~$
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:
sysadmin@localhost:~$ echo "The path is $PATH" The path is /usr/bin/custom:/home/sysadmin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games sysadmin@localhost:~$
4.12.2 Single Quotes
Single quotes prevent the shell from doing any interpreting of special characters. This includes globs, variables, command substitution and other metacharacter that have not been discussed yet.
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:
sysadmin@localhost:~$ echo The car costs $100 The car costs 00 sysadmin@localhost:~$ echo 'The car costs $100' The car costs $100 sysadmin@localhost:~$
4.12.3 Backslash Character (\)
You can use an alternative technique to essentially single quote a
single character. For example, suppose you want to print the following: “The services costs $100 and the path is $PATH
“. If you place this in double quotes, $1
and $PATH
are considered variables. If you place this in single quotes,$1
and $PATH
are not variables. But what if you want to have $PATH
treated as a variable and $1
not?
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:/usr/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:/usr/bin:/sbin:/bin:/usr/games sysadmin@localhost:~$
4.12.4 Back Quotes
Back quotes are used to specify a command within a command, a process called command substitution. This allows for very powerful and sophisticated use of commands.
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:
sysadmin@localhost:~$ echo Today is date Today is date sysadmin@localhost:~$
In the previous command the word date
is treated as regular text and the shell simply passes date
to the echo
command. But, you probably want to execute the date
command and have the output of that command sent to the echo
command. To accomplish this, you would run the echo Today is `date`
command line:
sysadmin@localhost:~$ echo Today is `date` Today is Mon Nov 2 03:40:04 UTC 2015 sysadmin@localhost:~$
4.13 Control Statements
Control statements allow you to use multiple commands at once or run additional commands, depending on the success of a previous command. Typically these control statements are used within scripts, but they can also be used on the command line as well.
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:
sysadmin@localhost:~$ cal 1 2015; cal 2 2015; cal 3 2015 January 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 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
4.13.2 Double Ampersand (&&)
The double ampersand &&
acts as a logical “and” if the first command is successful, then the second command (to the right of the &&
) will also run. If the first command fails, then the second command will not run.
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/xml
command 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:
sysadmin@localhost:~$ ls /etc/xml && echo success catalog catalog.old xml-core.xml xml-core.xml.old success sysadmin@localhost:~$ ls /etc/junk && echo success ls: cannot access /etc/junk: No such file or directory sysadmin@localhost:~$
In the first example above, the echo
command executed because the ls
command succeeded. In the second example, the echo
command wasn’t executed because the ls
command failed.
4.13.3 Double Pipe
The double pipe ||
is a logical “or”. It works in a similar way to &&
; depending on the result of the first command, the second command will either run or be skipped.
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 ls
command fails:
sysadmin@localhost:~$ ls /etc/xml || echo failed catalog catalog.old xml-core.xml xml-core.xml.old sysadmin@localhost:~$ ls /etc/junk || echo failed ls: cannot access /etc/junk: No such file or directory failed sysadmin@localhost:~$