You are on page 1of 43

Working with the BASH shell

Unit objectives
 Redirect the input and output of a command
Course ILT

 Identify, manipulate, create, and export shell


variables and edit environment files to
create variables
 Describe the purpose of shell scripts, create
and execute them, and use common
decision constructs in shell scripts
 Use and customize features of the BASH
shell
Topic A
 Topic A: Command input and output
 Topic B: Shell variables
Course ILT

 Topic C: Shell scripts


 Topic D: BASH command history
The BASH shell
 Provides a user interface
Course ILT

 Interprets commands
 Can manipulate command input and
output using shell metacharacters
File descriptors
 Standard Input (stdin)
– Represent information input to a
Course ILT

command during execution


 Standard Output (stdout)
– Represents the desired output from a
command
 Standard Error (stderror)
– Represents any error message
generated by a command
Course ILT Three common file descriptors
Redirection
 Redirect Standard Output and
Standard Error to a file using the “>”
Course ILT

 You can redirect both Standard Output


and Standard Error to separate files at
the same time
 You can redirect both Standard Output
and Standard Error to the same file by
using the “&” character
Course ILT Types of redirection
Course ILT Activity A-1

Redirecting Standard Input, Output,


and Error
Pipes
 Sends Standard Output of one
command as Standard Input to
Course ILT

another command
 Pipe
– A string of commands connected by the
“|” metacharacter
Course ILT Piping information
Course ILT Using multiple pipes
Course ILT Common filter commands
Course ILT Activity A-2

Using the pipe metacharacter


Topic B
 Topic A: Command input and output
 Topic B: Shell variables
Course ILT

 Topic C: Shell scripts


 Topic D: BASH command history
Shell variables
 Variable
– A reserved portion of memory containing
Course ILT

information that may be accessed


 Environment variables
– Typically set by the system
– Contain information that the system and
programs access regularly
 User-defined variables
– Your own custom variables
Environment variables
 Set by default in the BASH shell
 The set command
Course ILT

– Displays a list of environment variables


and their current values
Course ILT BASH environment variables
User-defined variables
 Variable identifier
– The name of a variable
Course ILT

 Features of variable identifiers


– Can contain alphanumeric characters,
the dash character, or the underscore
character
– Must not start with a number
– Typically capitalized
Subshells

 Most shell scripts that are run by the


shell are run in a separate subshell
Course ILT

 The subshell is created by the current


shell
 Variables created in the current shell
are not available to subshells or the
commands running within them
Course ILT Activity B-1

Discussing shell variables


Built-in commands
 Other variables not displayed by the
set or env commands
Course ILT

– Perform specialized functions in the shell


 The UMASK variable
– A special variable is set by the umask
command
Environment files
 Store defined variables
 Can define different variables for
Course ILT

different users
 Load variables at log in
Environment file execution
Order:
1. /etc/profile
Course ILT

2. ~/.bash_profile
3. ~/.bash_login
4. ~/.profile
Course ILT Activity B-2

Creating and using aliases


Topic C
 Topic A: Command input and output
 Topic B: Shell variables
Course ILT

 Topic C: Shell scripts


 Topic D: BASH command history
Shell scripts
 Text files that contain a list of
commands or constructs for the shell
Course ILT

to execute in order
 Can enter commands just as a user
would
Escape sequences

 Character sequences that have special


meaning inside the echo command
Course ILT
Reading Standard Input
 Shell script may need input from the
user
Course ILT

– Input may be stored in a variable for later


use
 The read command
– Takes input from Standard Input and
places it in a variable specified by an
argument to the read command
Course ILT Activity C-1

Creating and executing a shell script


Course ILT Decision constructs

continued
Course ILT Decision constructs, continued
if construct rules
– elif (else if) and else statements are
optional
Course ILT

– Can have unlimited number of elif


statements
– do these commands section can consist
of multiple commands
– do these commands section is typically
indented from the left
– End of statement is backward if, “fi”
– this is true part of syntax may be
command or test statement
Course ILT Common test statements
Course ILT Special operators
The case construct
 The case statement
– Compares variable value with several
Course ILT

patterns of text or numbers


– If there is a match, commands to the
right are executed
 Ended by a backward case, “esac”
The && and || constructs
 Shortcut constructs that take less time
from making a decision
Course ILT

 Syntax
– command && command
– command || command
Course ILT Activity C-2

Using decision constructs in a shell


script
Topic D
 Topic A: Command input and output
 Topic B: Shell variables
Course ILT

 Topic C: Shell scripts


 Topic D: BASH command history
The BASH history feature
 Recalls previous commands
 Reduces the number of number of
Course ILT

keystrokes
Course ILT Activity D-1

Using the BASH command history


Customizing the BASH history
 Update the bash_history file
 Specify number of commands to
Course ILT

remember
 Set HISTSIZE environment variable
Course ILT Activity D-2

Customizing the BASH history


Unit summary
 Redirected the input and output of
commands
Course ILT

 Identified, manipulated, created, and


exported shell variables; and edited
environment files to create variables
 Described the purpose of shell scripts,
executed scripts, and used common
decision constructs in shell scripts
 Used and learned how to customize
the BASH shell

You might also like