Linux Programming Lab
Module 05
Command Line Skills
Exam Objective
2.1 Command Line Basics
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 CLI provides more precise control, greater speed and the ability to
automate tasks more easily through scripting.
● The shell is the command line interpreter that translates commands entered
by a user into actions to be performed by the operating system.
● The Linux environment allows the use of many different shells.
● 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:~$
● The prompt shown contains the following information:
○ Username (sysadmin)
○ System name (localhost)
○ Current Directory (~)
The ~ symbol is used as shorthand for the user's home directory.
Commands
Commands
● A command is a software program that when executed on the CLI, performs
an action on the computer.
● 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.
● This additional input comes in two forms: options and arguments.
○ Options are used to modify the core behavior of a command.
○ Arguments are used to provide additional information (such as a filename or a
username).
● The typical format for a command is as follows:
command [options] [arguments]
Arguments
Commands
command [options] [arguments]
● An argument can be used to specify something for the command to act
upon.
● If the ls command is given the name of a directory as an argument, it lists
the contents of that directory:
sysadmin@localhost:~$ ls /etc/ppp
ip-down.d ip-up.d
● Some commands (such as ls) accept multiple arguments:
sysadmin@localhost:~$ ls /etc/ppp /etc/ssh
Options
Options
command [options] [arguments]
● 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
drwxr-xr-x 1 sysadmin sysadmin 0 Jan 29 2015 Desktop
drwxr-xr-x 1 sysadmin sysadmin 0 Jan 29 2015 Documents
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
● To execute the most recent command type !! and hit Enter:
● To execute the most recent iteration of a specific command, type !command and hit
Enter.
Variables
Variables
● A variable is a feature that allows the user or the shell to store data.
● Variables are given names and stored temporarily in memory.
● 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.
● To set the value of a variable, use the following assignment expression.
variable=value
● 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:
sysadmin@localhost:~$ echo $variable1
Something
Environment Variables
● Environment variables, also called global variables, are available system-wide.
● Examples include the PATH, HOME, and HISTSIZE variables.
● The command in the example below displays the value of the HISTSIZE variable:
sysadmin@localhost:~$ echo $HISTSIZE
1000
● The env command outputs a list of the environment variables.
● The export command is used to turn a local variable into an environment variable.
sysadmin@localhost:~$ export variable1
sysadmin@localhost:~$ env | grep variable1
variable1=Something
● Exported variables can be removed using the unset command:
Path Variable
● One of the most important Bash shell variables to understand is
the PATH variable.
● The PATH variable lists all the places that the system can look for
programs to execute.
● 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
● 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.
● The type command identifies the cd command as an internal command:
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.
● The which command searches for the location of a command by searching
the PATH variable.
sysadmin@localhost:~$ which ls
/bin/ls
sysadmin@localhost:~$ which cal
/usr/bin/cal
External Commands
● External commands can be executed by typing the complete path to the
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
● To display all locations that contain the command name, use the -a option to
the type command:
sysadmin@localhost:~$ type -a echo
Aliases
● An alias can be used to map longer commands to shorter key sequences.
● For example, the command ls -l is commonly aliased to l or ll.
● 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...
● The type command can identify aliases to other commands:
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):
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 (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.
sysadmin@localhost:~$ echo The car costs $100
The car costs 00
sysadmin@localhost:~$ echo 'The car costs $100'
The car costs $100
Backslash Character
● 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"
T
he service costs and the path is
/usr/bin/custom:/home/sysadmin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/
bin:/usr/games
● What if you want to have $PATH treated as a variable and $1 not?
● In this case, use a backslash \ character in front of the dollar sign $ character to
prevent the shell from interpreting it:
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
Backquotes
● Backquotes, or backticks, are used to specify a command within a command,
a process called command substitution.
● Note the output of the echo Today is date command line:
sysadmin@localhost:~$ echo Today is date
Today is date
● To execute the date command so the output of that command is sent to
the echo command, put the date command inside of two backquotes:
sysadmin@localhost:~$ echo Today is `date`
Today is Mon Nov 4 03:40:04 UTC 2030
Control Statements
Control Statements
● Control statements allow you to use multiple commands at once or run
additional commands.
● Control statements include:
○ Semicolon (;)
○ Double ampersand (&&)
○ Double pipe (||)
Control Statements
● The semicolon can be used to run multiple commands, one after the other:
sysadmin@localhost:~$ cal 1 2015; cal 2 2015; cal 3 2015
● 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:
sysadmin@localhost:~$ ls /etc/xml && echo success
catalog catalog.old xml-core.xml xml-core.xml.old
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:
sysadmin@localhost:~$ ls /etc/junk || echo failed
ls: cannot access /etc/junk: No such file or directory
failed
Practices
Practice 1: Chapter 5 Exam
Login to your Netacad and do Chapter 5 exam
30 min max
Practice 2: Lab 5
Login to your Netacad and Perform Lab 5
This is Lab 5: Command Line Skills. By performing this lab, you will learn
how to use basic features of the shell.
In this lab, you will perform the following tasks:
• Explore Bash features
• Use shell variables
• Be able to make use of quoting