The Bash Shell
• Why use the Bash Shell and the CLI?
– 1. Control – the command line permits users to include options
that provide both flexibility and power over the defaults when
issuing commands by GUI.
– 2. Speed – many programs issued at the command line are textbased and therefore will start faster and run faster than GUIbased programs.
– 3. Resources – as with speed, a text-based program uses fewer
system resources.
– 4. Remote access – if you are using a program like ssh to reach a
computer, you may be limited to a text–based interface, so
learning it is important and once learned, you gain the other
– 5. Reduced health risk – yes, this one sounds odd, but mouse
usage is one of the most harmful aspects of using a computer as
it strains numerous muscles in your hand and wrist, while typing
is less of a strain (although poor hand placement can also
damage your wrist).
The Command Line Prompt
• You can tailor your prompt but by default, it
will look like this:
– [[email protected] ~]$
• What do these parts mean?
– Username
– Hostname
– Current directory
• ~ means “user’s home directory”
– $ the user prompt (# for root)
Entering Commands
• Type the name of your command and press
• Some commands do not require additional
information, so the command is just the name of
the command itself (which is also a program’s
– The following instructions output information similar
to what we find from our prompt
• whoami – output the user’s username
• pwd – output the current working directory
• hostname – output the name of the host (this will either be
localhost.localdomain or the IP address or IP alias of the
machine logged in to)
• ls – list the contents of the current working directory
Other Commands
• passwd – used to change your password
– you are first prompted to input your current password and then your
new password (twice). If the password you enter is not sufficiently
strong, you are warned
• uname – output basic information about your operating system
• arch – output basic information about your computer’s hardware
• who – list the users currently logged in to the computer you are
operating on, including their terminal
– which terminal window they have opened or whether they are on
• bash – start a new Bash shell (if in a shell, this starts a new
session so that the outer shell session is hidden)
– sh starts a new shell in the default shell type which may not be Bash
• exit – leave the current Bash shell and if this is the outermost
Bash shell, close the window
Example Session
user input
program output
[[email protected] ~]$ uname
[[email protected] ~]$ arch
[[email protected] ~]$ who
foxr tty7 2013-10-11 09:42 (:0)
foxr pts/0 2013-10-11 15:14 (:0)
[[email protected] ~]$ uname; arch; who
foxr tty7 2013-10-11 09:42 (:0)
foxr pts/0 2013-10-11 15:14 (:0)
Commands with Options/Parameters
• Most Linux commands allow or require
additional information
– Options – user-specified variations in how the
command should be performed
– Parameters – files/directories that the command
should operate on
• The typical format for a command is
– command [option(s)] [parameter(s)]
• The [ ] indicate “optional”
The ls Command
• The ls (list) command lists the contents of a
directory or information about directories and
– examples
ls – list all contents
ls –l – perform a long listing (-l is lower case L)
ls –l file1.txt – perform long listing on file1.txt
ls –l file1.txt file2.txt file3.txt – perform long listing on
several files
• ls –l *.txt – perform long listing on all items in this directory
that end with .txt
• ls –a – show all files including “hidden” files
• ls –al (also ls –la and ls –a –l) – both ls –a and ls –l combined
The ls Command: Long Listings
• A long listing will display for each item
– the type of object (file, directory, symbolic link, etc where a
hyphen means a regular file)
– the permissions of the object (who can read, write and
execute it, which appear using r, w, and x, with a hyphen
indicating no permission, we examine permissions in detail
in chapter 3)
– the number of names linked to the object
– the user owner and group owner of the object
– the size of the object
– the last modification (or creation) date and time of the
– the name of the object (and if a symbolic link, the item it is
linked to)
The ls Command: Long Listings
Example long listing of a part of a directory, all of these are files with the
same permissions, number of hard links, owner, group, creation date
The ls Command: Other Options
Same as –a exact that . and .. are not shown
Ignore items whose names end with ~ (~ is used for backup files)
List entries in columns (fits more items on the screen)
List directories by name, do not list their content
Append listings with item classifications (ends directory names with /, ends executable files with *)
Same as –l except that owner is not shown
Same as –l except that group owner is not shown
When used with –l, modifies file sizes to be “human readable”
Include inode number of items (inodes are discussed in chapter 10)
Dereference links – that is, display information about item being linked to, not the link itself (links
are discussed in chapters 3 and 10)
List items in reverse alphabetical order
Recursive listing (list contents of all subdirectories)
When used with –l, outputs sizes in blocks rather than bytes
Sort files by size rather than name
Sort files by modification time rather than name
Sort files by extension name rather than name
man Pages
• man – command to list a command’s man page
– gives information about the command
man Pages: Types of Information
Command name with one line description
One or more entries that show the syntax of the command’s usage
Detailed description of what the command does
Description of every available option, including additional parameters expected
by options, and options that options can have (options may be listed under
Exit or return values
Additional parameters expected or allowed
Environment variables that this command might use
Known errors or fixes
Additional information that might be useful
Files that this command might access
Error or exit codes
In rare cases, some man pages include examples of how to use the command
Names of functions that can be called by the command
Person/people who wrote the command’s programming code
See also
Other Linux instructions related to this one or worth exploring
man Pages: Moving Through Them
forward one screen – ‘f’, ‘z’, space bar
forward half a screen – ‘d’
forward one line – ‘e’, ‘j’, enter key, down arrow
back one screen – ‘b’, ‘w’
back one half screen – ‘u’
back one line – ‘b’, up arrow
return to top of man page – ‘g’
go to bottom of man page – ‘G’
go to line # - #G (# is a number like 10G for line 10)
obtain help – ‘h’, ‘H’
to move forward # lines - # (# is a number)
to search forward for a string - /string <enter>
to search backward for a string - ?string <enter>
exit man page – ‘q’, ‘Q’
The apropos Command
• Used to list all commands whose description
matches a given string
– the description is taken from all of the man pages
– format: apropos string or apropos “string”
• if you use “” then the string must match exactly whereas if
you don’t and the string is multiple words, then apropos
matches descriptions that contain any of the words
– example: apropos virtual memory lists over 100
instructions while apropos “virtual memory” lists
• mremap (2)
• vfork (3p)
• vmstat (8)
- re-map a virtual memory address
- create a new process; share virtual memory
- Report virtual memory statistics
Bash Features: History
• Every instruction entered is stored in a history
• You can recall instructions from the history list
to save time from re-typing a previous
• To obtain your history, use the history
– history – lists the previous n instructions where n
is a default value
– history n – list the last n instructions in the history
Bash Features: History
• Given the history list on the right, how
do we recall instructions?
– control+p or up arrow to backward
through the history list
– control+n or down arrow to move
forward through the history list
– !! – re-execute last instructino
– !# - re-execute instruction # (e.g., !10 to
repeat cat script1)
– !str – re-execute most recent instruction
that starts with the string str (!c reexecutes 10 while !cd re-executes 8)
1 ls
2 cd ~
3 pwd
4 man cd
5 ls –l
6 cd /bin
7 ls –l | grep root
8 cd ~
9 vi script1
10 cat script1
11 rm script1
12 history
Bash Features: History
• Other history options aside from n
• -c – clear the history list
• -d # – where # is a number, delete that numbered
entry from the history list
• -w – write the current history list to the history
• -r – input the history file and use it instead of the
current history
• # – where # is a number, display only the last #
items in the list
Bash Features: Variables
• A variable is a name given to a storage
– In Bash, variables by default store strings but can
also store integer numbers (no decimal point) or
arrays of strings/integers
• Three types of variables
– Normal variables are defined from the command
line and available in your current session
– Script variables which are known only in the script
– Environment variables are defined in a script but
exported to be known in your environment
Bash Features: Variables
• You assign variables their values in an
assignment statement
– var=value
• var is the name of the variable which can consist of
letters, digits and underscores (_) as long as the name
does not start with a digit
• value is a literal value, the output returned from a Linux
program, the value stored in another variable, or the
result of some operation on the value stored in a
variable or any combination of these
• values which contain blank spaces must be entirely
enclosed within quote marks (“” or ‘’ although we
prefer “”)
Bash Features: Environment Variables
• These variables are pre-defined at the time the
user logs in and opens a shell
Meaning (value)
GUI being used (Gnome, KDE)
The terminal window being used, denoted as :0.0 if you are logged in through a GUI
Number of entries retained in the history list
User’s home directory name
Name of computer
User’s user name
Location of user’s email directory
Last directory visited before current directory (see PWD)
The list of directories that the interpreter checks with each command
Specifies the user’s prompt (explained in more detail in table 2.5)
The current directory
Name of the program being used as the user’s shell (for Bash, this is usually /bin/bash)
The terminal type being used
The user’s username
Bash Features: PATH Variable
• Whenever you type in a command, the
interpreter must locate the executable program
of that command
– Many are stored in /bin and /usr/bin
• To prevent the user from having to type in the
full path, such as /bin/ls or /usr/bin/apropos,
we use a variable called PATH
– This variable stores directories that the interpreter
will test in searching for the program
Bash Features: PS1 Variable
• PS1 encodes the information that is displayed
in your prompt
– for instance: [\[email protected]\h \d] $ is your current
User name
Host name
Current working directory
Number of current command (as it will appear in the history list)
Current time
Date (as in Mon Aug 13)
Status of last command
Bell character (makes a tone)
Number of jobs in the current shell (we explore jobs in chapter 4)
New line
Bash Features: echo
• The echo command is an output statement to
output the value stored in a variable
– Format: echo string
– If the string is to include variable names, put a $
before each variable name as in $var to output the
value stored in the variable
– If name=Richard then
• echo $name outputs Richard while echo name outputs
Bash Features: echo
• echo has a few options of note
– -n – do not output a new line character at the end
of the output so that the next echo statement
continues on the same line
– -e – enable \ escapes (see table below)
– -E – disable \ escapes
Escape Character
\! \$ \& \; \' \"
Output a \
Backspace (back cursor up
Newline – start a new line
!, $, &, ;, ' and " respectively
Bash Features: alias
• In Linux, an alias is a name applied to a command
• You define aliases
– to reduce the amount of typing
– so that complicated instructions do not have to be
– so that common typos made by the user can be
interpreted correctly
– so that dangerous commands are made safer
• Format: alias name=command
– If the command includes spaces, enclose the entire
command in ‘’
Bash Features: alias
• Examples
alias ..='cd ..'
less typing
alias lss=less
common typo
alias sl=ls
common typo
alias mountcd='mount /dev/cdrom /cd iso9660
long or hard to remember
– alias rm='rm –i'
• To remove an alias, type unalias name
– as in unalias sl
• To see all defined aliases, type alias by itself
Bash Feature: Command Line Editing
• Bash has a number of useful keystrokes that save
you from excessive typing on the CLI
• These are combinations of control or escape
– control+key means hold control and press key
– escape+key means press escape and then press key
• These sequences are based on the emacs editor
(see chapter 5)
• Learning these can save you a lot of time and
Bash Features: Command Line Editing
ctrl+n (also up arrow)
ctrl+p (also down arrow)
ctrl+f (also right arrow)
ctrl+b (also left arrow)
ctrl+d (also delete key)
Move cursor to beginning of line
Move cursor to end of line
Move to next instruction in history list
Move to previous instruction in history list
Move cursor one character to the right
Move cursor one character to the left
Delete character at cursor
Delete all characters from cursor to end of line
Delete everything from the command line
Delete all characters from front of word to cursor
Yank, return all deleted characters (aside from c+d) to cursor
Undo last keystroke
Move cursor to space after current word
Move cursor to beginning of current word
Delete the remainder of the word
Bash Features: Redirection
• By default, Linux programs obtain input from STDIN
(standard input) and send output to STDOUT
(standard output)
• You can alter this through redirection
– > - redirect output from terminal window to file (if file
already exists, it is deleted and a new one created)
– >> - redirect output from terminal window and append to
– < - redirect input from keyboard to file (nearly all Linux
instructions receive input from file so this is not usually
– << - redirect input from file to keyboard
– | - pipe, or redirect output to be input to another Linux
Bash Features: Redirection
• For instance, we want to combine several files
using the cat (concatenate) command
– cat file1.txt file2.txt file3.txt > joined_files.txt
• Without the redirection, the combined files are
displayed to the screen
Bash Features: Redirection
• Here we redirect the output of the ls command
of several users’ home directories to the file
– At first, we create the file anew and then we
append the remainder of the commands to the
same file
ls /home/dukeg > user_entries.txt
ls /home/marst >> user_entries.txt
ls /home/underwoodi >> user_entries.txt
ls /home/underwoodr >> user_entries.txt
ls /home/zappaf >> user_entries.txt
Bash Features: Redirection
• For input from keyboard, we use << as in
– cat <<
• now whatever we type from keyboard is echoed to the
terminal window
• but this includes <enter> so how do we end input? Use
• We can also use cat << string
– End input once you type string<enter>
• We might combine << and > to create a file of
text we enter by the command line
– cat << string > file.txt
Bash Features: Redirection
• The pipe is very useful to take output from one
program and feed it as the input to another
– ls | less
• Display the contents of the current directory but
use the less program to display it
– less displays 1 screen at a time and lets you scroll
forward and backward
• cat somefile | less
– Again, display the content 1 screen at a time
• cat << string | sort > file.txt
– like the previous example to create a file from
keyboard input but this time the input is sorted before
being stored to file
Bash Features: Wildcards
• The wildcard is a character used to match
against filenames (and directory names)
• The simplest wildcard to understand is * which
means “match anything”
– ls *.txt means list all files whose name ends in .txt
– ls * matches all items in the current directory
– ls a* matches all items that start with an a
• To other useful wildcards are ? (match any one
character) and [ ] (match any one character in
the brackets)
– We will explore these in more detail in chapter 3
Bash Features: Tab Completion
• The Bash interpreter can complete directory
and file names for you
– Type a portion of the name that is unique, press
<tab>, Bash completes it
– If the portion is not unique, Bash emits a beep
– Press <tab><tab> and Bash displays all
directories/files that match
• if there are too many, you are told this in a message like
Display all 153 possibilities? (y or n)
– Tab completion is very handy (if you remember to
use it!)
Special Files
• There are several files that define initial aliases,
functions and variables – these allow you to tailor
your shell as you like
• These are script files, automatically executed
when you first log in (/etc/profile, ~/.profile) or
open a (new) Bash shell (/etc/bashrc, ~/.bashrc,
– Some are located in /etc and can be modified by the
system administrator
– Some are located in the user’s home directory for the
user to modify
• If you modify a file, to have it take effect, use
source as in source .bashrc
Other Shells: csh
• The C shell uses syntax similar to the C
programming language
– Introduced numerous features now found in Bash
Command line editing
Directory stack (see chapter 3)
Escape completion (instead of tab completion)
Job control (see chapter 4)
Other Shells: TC Shell
• Updated version of C shell, T for TENEX
operating system, or tcsh
• Updated history commands
command !*
command !$
command !^
command !:n
command !:m-n
Execute previous command
Execute command n on the history list
Execute the most recent command that starts with the characters string
Execute the most recent command that contains the characters string
Execute command but use all arguments from the previous command
Execute command but use the last argument from the previous command
Execute command but use the first argument from the previous command
Execute command but use the nth argument from the previous command
Execute command but use arguments m-n from the previous command
Other Shells
• KornShell (ksh) released in 1983
– Includes WYSIWYG command line editing
• Almquist shell (ash) released in 1989
– Scaled down and faster version of Bourne shell
• Z Shell (zsh) released in 1990 adds spell
checking to features found in sh and ksh
Comparing Bash and TC Shell
User prompt
Assignment statement
Export statement
Alias definition format
Default user definition
Automated spelling
TC Shell
set var=value
export var=value
setenv var value
alias term=command alias term command
The Bash Interpreter
• Upon entering a command, the Bash interpreter
takes over performing the following steps in this
– The input is broken into individual tokens
• a token is a known symbol (e.g., <<, |, ~, *, !, etc) or
• a word separated by spaces where the word is a command or
• a word separated by spaces where the word is a filename,
directory name or path or
• an option combining a hyphen (usually) with one or more
characters (e.g., -l, -al).
– If the instruction has any quotes, those quotes are
– If an alias is found, that alias is replaced by the righthand side of the definition.
The Bash Interpreter
– The various words and operators and now broken up into
individual commands (if there are multiple instructions
separated by semicolons).
– Brace expansion unfolds into individually listed items.
– ~ is replaced by the appropriate home directory.
– Variables are replaced by their values.
– If any of the commands appears either in ` ` or $( ), execute
the command (we explore this in chapter 7).
– Arithmetic operations (if any) are executed.
– Redirections (including pipes) are performed.
– Wildcards are replaced by a list of matching file/directory
– The command is executed and, upon completion, the exit
status of the command (if necessary) is displayed to the
terminal window (if no output redirection was called for).