5.
1 Introduction
Linux - considers CLI as a powerful tool that accomplishes vast array of tats with single
command-line instruction
5.2 Shell
The shell is the command line interpreter that translates commands entered by a user into
actions to be performed by the operating system.
Bash - most commonly-used shell for Linux distribution
-can re-execute previously executed commands
Other popular features of Bash:
Scripting: The ability to place commands in a file and then interpret (effectively use Bash to
execute the contents of) 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: Used to store information for the Bash shell and for the user. These variables can be
used to modify how commands and features work as well as provide vital system information.
Prompt - to indicate that commands can be run, but it also conveys useful information to the
user.
-fully configurable and can be as sparse or as full-featured as is practical and useful
Prompt information:
User Name:
sysadmin@localhost:~$
System Name:
sysadmin@localhost:~$
Current Directory:
sysadmin@localhost:~$
~ - shorthand for the user’s home directory
Typically it is /home directory and named after the user account name
E.g. /home/sysadmin
5.3 Commands
Given input to the CLI that is to be executed and performed by the computer.
Some require an additional that comes in two forms: options and arguments
Format: command [options] [arguments]
Options are used to modify the core behavior of a command while arguments are used to
provide additional information.
Note: Linux is case-sensitive
ls - lists files and directories contained in the current working directory
An argument can be used to specify something for the command to act upon
E.g.
sysadmin@localhost:~$ ls /etc/ppp
ip-down.d ip-up.d
Shows the files contain in /etc/ppp directory
It also accepts multiple arguments
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
Shows the files contained in both /etc/ppp /etc/ssh
Options can be used with commands to expand or modify the way a command behaves
-l option of ls -results in a long listing, providing additional information about the files that are
listed (such as permission, the size, and other infos of the file)
E.g
sysadmin@localhost:~$ ls -l
total 0
drwxr-xr-x 1 sysadmin sysadmin 0 Jan 29 20:13 Desktop drwxr-
xr-x 1 sysadmin sysadmin 0 Jan 29 20:13 Documents drwxr-xr-x 1
sysadmin sysadmin 0 Jan 29 20:13 Downloads drwxr-xr-x 1 sysadmin
sysadmin 0 Jan 29 20:13 Music drwxr-xr-x 1 sysadmin sysadmin
0 Jan 29 20:13 Pictures drwxr-xr-x 1 sysadmin sysadmin 0 Jan 29
20:13 Public drwxr-xr-x 1 sysadmin sysadmin 0 Jan 29 20:13
Templates drwxr-xr-x 1 sysadmin sysadmin 0 Jan 29 20:13 Videos
-r option of ls, display the results in an reverse order
E.g.
sysadmin@localhost:~$ ls -r
Videos Templates Public Pictures Music Downloads Documents Desktop
n most cases, options can be used in conjunction with other options. They can be given as
separate options, as in -l -r, or combined, as in -lr
E.g.
sysadmin@localhost:~$ ls -lr
total 32
drwxr-xr-x 2 sysadmin sysadmin 4096 Oct 31 20:13 Videos
drwxr-xr-x 2 sysadmin sysadmin 4096 Oct 31 20:13 Templates
drwxr-xr-x 2 sysadmin sysadmin 4096 Oct 31 20:13 Public
drwxr-xr-x 2 sysadmin sysadmin 4096 Oct 31 20:13 Pictures
drwxr-xr-x 2 sysadmin sysadmin 4096 Oct 31 20:13 Music
drwxr-xr-x 2 sysadmin sysadmin 4096 Oct 31 20:13 Downloads
drwxr-xr-x 4 sysadmin sysadmin 4096 Oct 31 20:13 Documents
drwxr-xr-x 2 sysadmin sysadmin 4096 Oct 31 20:13 Desktop
Order is not important. The output will still be the same when the following options are entered
ls -l -r
ls -rl
ls -lr
By default the -l option of the ls command displays files sizes in bytes:
sysadmin@localhost:~$ ls -l /usr/bin/perl -rwxr-xr-
x 2 root root 10376 Feb 4 2018 /usr/bin/perl
If the -h option is added the file sizes will be displayed in human-readable format:
sysadmin@localhost:~$ ls -lh /usr/bin/perl -rwxr-xr-
x 2 root root 11K Feb 4 2018 /usr/bin/perl
Options are often single letters; however, sometimes they are words or phrases as well.
Typically, older commands use single letters while newer commands use complete words
for options.
Full word options use two dash -- before the option
E.g
sysadmin@localhost:~$ ls -l --human-readable /usr/bin/perl
-rwxr-xr-x 2 root root 11K Feb 4 2018 /usr/bin/perl
5.3.3 History
When a command is executed in the terminal, it is stored in a history list. Pressing the Up Arrow
↑ key displays the previous command on the prompt line.
To view the history list of a terminal, use the history command
If the desired command is in the list that the history command generates, it can be executed by
typing an exclamation point ! character and then the number next to the command.
sysadmin@localhost:~$ history
1 date
2 ls
3 cal 5 2030
4 history
sysadmin@localhost:~$ !3
cal 5 2030
May 2030
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
If the history command is passed a number as an argument, it outputs that number of previous
commands from the history list.
sysadmin@localhost:~$ history 3
6 date
7 ls /home
8 history 3
To execute the nth command from the bottom of the history list, type !-n and hit Enter.
sysadmin@localhost:~$ !-3
date
Wed Dec 12 04:31:55 UTC 2018
To execute the most recent command type !! and hit Enter:
sysadmin@localhost:~$ date
Wed Dec 12 04:32:36 UTC 2018
sysadmin@localhost:~$ !!
date
Wed Dec 12 04:32:38 UTC 2018
5.4 Variables
A variable is a feature that allows the user or the shell to store data
-Variables can be used to provide critical system information or to change the behavior of how
the Bash shell (or other commands) work.
-are given names and stored temporarily in a memory
Two types of Variables:
5.4.1 Local Variables
- exist only in the current shell, and cannot affect commands or applications
- when terminal or shell is closed, all of the variables are lost
-user-based tasks and lowercase in convention
Syntax:
variable=value
The following example creates a local variable named variable1 and assigns it a value of
Something:
sysadmin@localhost:~$ variable1='Something'
The echo command is used to display output in the terminal. To display the value of the variable,
use a dollar sign $ character followed by the variable name as an argument to the echo
command:
sysadmin@localhost:~$ echo $variable1
Something
5.4.2 Environment Variables
-also called global variables
-are available system-wide, in all shell used by Bash when interpreting and performing tasks
-The system automatically recreates environment variables when a new shell is opened
E.g. PATH, HOME, and HISTSIZE variables.
The HISTSIZE variable defines how many previous commands to store in the history list
sysadmin@localhost:~$ echo $HISTSIZE
1000
To modify the value of an existing variable, use the assignment expression:
sysadmin@localhost:~$ HISTSIZE=500
sysadmin@localhost:~$ echo $HISTSIZE
500
the env command outputs a list of the environment variables
The pipe | character passes the output of the env command to the grep command, which
searches the output.
The export command is used to turn a local variable into an environment variable.
E.g.
export variable
The export command can also be used to make a variable an environment variable upon its
creation by using the assignment expression as the argument:
sysadmin@localhost:~$ export variable2='Else'
sysadmin@localhost:~$ env | grep variable2
variable2=Else
To change the value of an environment variable, use the assignment expression:
sysadmin@localhost:~$ variable1=$variable1' '$variable2
sysadmin@localhost:~$ echo $variable1
Something Else
Exported variables can be removed using the unset command:
sysadmin@localhost:~$ unset variable2
5.4.3 Path Variable
- It contains a list that defines which directories the shell looks in to find commands
- If a valid command is entered and the shell returns 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.
sysadmin@localhost:~$ echo $PATH
/
home/sysadmin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/
bin:/usr/games
sysadmin@localhost:~$
Each directory in the list is separated by a colon : character. Based on the preceding output, the
path contains the following directories. The shell will check the directories in the order they are
listed:
/home/sysadmin/bin
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin
/usr/games
If the command is not found in any directory listed in the PATH variable, then the shell returns an
error
sysadmin@localhost:~$ zed
-bash: zed: command not found
sysadmin@localhost:~$
If custom software is installed on the system it may be necessary to modify the PATH to make it
easier to execute these commands. For example, the following will add and verify 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
When updating the PATH variable, always include the current path, so as not to lose access to
commands located in those directories. This can be accomplished by appending $PATH to the
value in the assignment expression. Recall that a variable name preceded by a dollar sign
represents the value of the variable.
5.5 Command Type
type - a command that can be used to determine information the command type
e.g. type command
5.5.1 Internal Commands
-also called built-in commands
-commands built into the shell itself
cd (change directory) command - example of a built-in command
5.5.2 External Commands
-stored in files that are searched by the shell
-when an external command, for example ls, is used the shell shall search the directories listed in
the PATH and try to find the ls command
which command - displays the location of the command by searching the PATH variable
E.g. which command
External commands can also be executed by typing the complete path to the command. For
example, to execute the ls command:
sysadmin@localhost:~$ /bin/ls
Desktop Documents Downloads Music Pictures Public Templates Videos
For external commands, the type command displays the location of the command:
sysadmin@localhost:~$ type cal
cal is /usr/bin/cal
In some cases the output of the type command may differ significantly from the output of the
which command:
Using the -a option of the type command displays all locations that contain the command
named:
sysadmin@localhost:~$ type -a echo
echo is a shell builtin
echo is /bin/echo
5.5.3 Aliases
- can be used to map longer commands to shorter key. When the shell sees an alias being
executed, it substitutes the longer sequence before proceeding to interpret commands.
New aliases can be created using the following format, where name is the name to be given the
alias and command is the command to be executed when the alias is run.
alias name=command
Aliases are only saved when shell is open. When it is closed, all the new aliases are lost.
5.5.4 Functions
can also be built using existing commands to either create new commands, or to override
commands built-in to the shell or commands stored in files. Aliases and functions are normally
loaded from the initialization files when the shell first starts.
5.6 Quoting
Used to let the system know that the information contained within quotation marks should either
be ignored or treated in a way that is very that it would normally be treated
3 types of quotes
Double quotes
Single quotes
Back quotes
5.6.1 Double quotes
-stop the shell from interpreting some metacharacter (special characters), including glob
characters with the exception of $, `, \, and, when history expansion is enabled, !.
Glob characters - also called wild cards, are symbols that have special meaning to the shell (e.g.
*, ?, [ ])
-when a series of texts and characters are double-quoted, they become mere texts used to
display on the screen
sysadmin@localhost:~$ echo "The glob characters are *, ? and [ ]"
The glob characters are *, ? and [ ]
Double quotes still allow for command substitution, variable substitution, and permit some other
shell metacharacters that haven't been discussed yet. The following demonstration shows that
the value of the PATH variable is still 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
5.6.2 Single Quotes
Single quotes prevent the shell from doing any interpreting of special characters, including globs,
variables, command substitution and other metacharacters that have not been discussed yet.
For example, to make the $ character simply mean a $, rather than it acting as an indicator to the
shell to look for the value of a variable, 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
5.6.4 Backslash Character
-used to prevent a character from being interpreted by the shell
In this case, use a backslash \ character in front of the dollar sign $ character to prevent the
shell from interpreting it. The command below demonstrates using the \ character:
sysadmin@localhost:~$ echo The service costs \$1 and the path is $PATH
The service costs $1 and the path is
/usr/bin/custom:/home/sysadmin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin
:/usr/bin:/sbin:/bin:/usr/games
5.6.4 Backquotes
- also called as backsticks
Backquotes, or backticks, are used to specify a command within a command, a process called
command substitution. This allows for 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 4 03:35:50 UTC 2018
Note: $( ) can also be used as a substitution for backsticks
Now, note the output of the echo command:
sysadmin@localhost:~$ echo Today is date
Today is date
In the previous command, the word date is treated as regular text, and the shell passes date to
the echo command. To execute the date command and have the output of that command sent to
the echo command, put the date command in between two backquote characters:
sysadmin@localhost:~$ echo Today is `date`
Today is Mon Nov 4 03:40:04 UTC 2018
5.7 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.
5.7.1 Semicolon
command1; command2; command3
The semicolon ; character can be used to run multiple commands, one after the other.
Each command runs independently and consecutively; regardless of the result of the first
command, the second command runs once the first has completed, then the third and so
on.
5.7.2 Double Ampersand
command1 && command2
The double ampersand && acts as a logical "and"; if the first command is successful, then the
second command 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 command. The command succeeds if the given directory is
accessible and fails if it isn't.
5.7.3 Double Pipe
command1 || command2
The double pipe || is a logical "or". 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 is 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 only executes if the ls command fails:
sysadmin@localhost:~$ ls /etc/ppp || echo failed
ip-down.d ip-up.d
sysadmin@localhost:~$ ls /etc/junk || echo failed
ls: cannot access /etc/junk: No such file or directory
Failed
LAB 5
The prompt tells you that you are user sysadmin; the host or computer you are using:
localhost; and the directory where you are at: ~, which represents your home directory.
sysadmin@localhost:~$
Equally important is the command line syntax, which is the order in which the command, the
option(s), and argument(s) must be entered into the prompt so the shell recognizes how to
properly execute the command. Proper command line syntax looks like the following:
command [options] [arguments]
sysadmin@localhost:~$ whoami
sysadmin
sysadmin@localhost:~$
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.
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
Your output should be similar to the following:
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 uname command again twice in the terminal, once with the option -n and again with
the option --nodename. This will display the network node hostname, also found in the prompt.
uname -n
uname --nodename
Your output should be similar to the following:
sysadmin@localhost:~$ uname -n
localhost
sysadmin@localhost:~$ uname --nodename
localhost
The pwd command is used to display your current "location" or current "working" directory. Type
the following command to display the working directory:
pwd
Your output should be similar to the following:
sysadmin@localhost:~$ pwd
/home/sysadmin
sysadmin@localhost:~$
/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.
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.