Professional Documents
Culture Documents
Objective Description
Basics of Using the Linux Command Line
Introduction
Introduction
● This module will cover the basics of the command line such as:
○ The command line interface
○ The shell
○ Commands
○ Variables
○ Command Types
○ Quoting
○ Control Statements
Command Line Interface
Command Line Interface
● The Linux community promotes the CLI due to its power, speed and ability to
accomplish a vast array of tasks with a single command line instruction.
● The CLI provides more precise control, greater speed and the ability to automate
tasks more easily through scripting.
● By learning the CLI, a user can easily be productive almost instantly on ANY flavor or
distribution of Linux
The Shell
The Shell
● Once a user has entered a command the terminal then accepts what the user has
typed and passes to a shell.
● The shell is the command line interpreter that translates commands entered by a
user into actions to be performed by the operating system.
● The most commonly used shell for Linux distributions is called the Bash shell.
The Shell
● The Bash shell also has many popular features, a few of which are listed
below:
○ Command line history
○ Inline editing
○ 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.
○ Aliases
■ The ability to create short nicknames for longer commands.
○ Variables
■ Used to store information for the Bash shell and for the user.
The Shell
● When a terminal application is run, and a shell appears, displaying an important part
of the interface — the prompt.
● Typically the prompt contains information about the user and the system. Below is a
common prompt structure:
sysadmin@localhost:~$
● To execute a command, the first step is to type the name of the command.
● If you type ls and hit Enter. The result should resemble the example below:
sysadmin@localhost:~$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
Commands
● Some commands require additional input to run correctly.
● An argument can be used to specify something for the command to act upon.
● Options can be used with commands to expand or modify the way a command behaves.
● For example, using the -l option of the ls command results in a long listing, providing additional
information about the files that are listed.
sysadmin@localhost:~$ ls -l
total 0
Output Omitted...
● Often the character is chosen to be mnemonic for its purpose, like choosing the letter l for long
or r for reverse.
Options
● Options can be used in conjunction with other options:
sysadmin@localhost:~$ ls -lr
● 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.
○ Single-letter options are preceded by a single dash - character, like the -h option.
○ Full-word options are preceded by two dash -- characters like the full-word form of the -h
option, the --human-readable option
Command History
● When a command is executed in the terminal, it is stored in a history list.
● This makes it easy to execute the same command later eliminating the need to
retype the entire command.
● Pressing the Up Arrow ↑ key displays the previous command on the prompt line.
● To view the entire history list of a terminal, use the history command:
sysadmin@localhost:~$ history
1 date
2 ls
3 cal 5 2030
4 history
Command History
● 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 (i.e., !3)
● 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
● There are two types of variables used in the Bash shell, local and
environment.
Local Variables
● Local or shell, variables exist only in the current shell. When the user closes a terminal window
or shell, all of the variables are lost.
● The following example creates a local variable named variable1 and assigns it a value of
Something:
sysadmin@localhost:~$ variable1='Something'
● To display the value of the variable, use a dollar sign $ character followed by the variable name
as an argument to the echo command:
● The command in the example below displays the value of the HISTSIZE variable:
1000
● The export command is used to turn a local variable into an environment variable.
sysadmin@localhost:~$ export variable1
variable1=Something
● If the command is not found in any directory listed in the PATH variable,
then the shell returns a command not found error.
Command Types
Command Types
● The type command can be used to determine information about command type.
type command
● There are several different sources of commands within the shell of your CLI:
○ Internal commands
○ External commands
○ Aliases
○ Functions
Internal Commands
● Also called built-in commands, these commands are built into the shell itself.
● A good example is the cd (change directory) command as it is part of the Bash shell.
sysadmin@localhost:~$ type cd
cd is a shell builtin
External Commands
● External commands are stored in files that are searched by the shell.
● It can be beneficial to know where the shell is finding the command or which version
it is using.
sysadmin@localhost:~$ which ls
/bin/ls
/usr/bin/cal
External Commands
● External commands can be executed by typing the complete path to the command.
sysadmin@localhost:~$ /bin/ls
● For external commands, the type command displays the location of the command:
cal is /usr/bin/cal
● To display all locations that contain the command name, use the -a option to the
type command:
● To determine what aliases are set on the current shell use the alias command:
sysadmin@localhost:~$ alias
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
Output Omitted...
sysadmin@localhost:~$ type ll
ll is aliased to `ls -alF'
Functions
● Functions can also be built using existing commands to:
○ Create new commands
○ 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.
Quoting
Double Quotes
● Double quotes stop the shell from interpreting some metacharacters, including glob
characters.
Glob characters, also called wild cards, are symbols that have special meaning to the shell (i.e, *, ?).
● This is useful when you want to display something on the screen that is normally a
special character to the shell.
● In the example below, the Bash shell doesn't convert the glob pattern into filenames
that match the pattern (like it normally does):
● Double quotes still allow for command substitution, variable substitution, and permit
some other shell metacharacters (i.e., the PATH variable)
Single Quotes
● Single quotes prevent the shell from doing any interpreting of special characters,
including globs, variables, command substitution and other metacharacters.
● A technique to essentially single quote a single character is to use the backslash character \.
● If the phrase below is placed in single quotes, $1and $PATH are not variables:
sysadmin@localhost:~$ echo "The service costs $1 and the path is $PATH"
sysadmin@localhost:~$ echo The service costs \$1 and the path is $PATH
Today is date
● 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:
success
● The double pipe || is a logical "or". It works similarly to &&; depending on the result of the first
command, the second command will either run or be skipped:
failed