UNIVERSITY OF LUCKNOW
BACHELOR OF COMPUTER APPLICATION
YEAR: THIRD,
SEMESTER –V
BCA-504
Unix and Shell Programming
Unit 4:The Shell
Overview of the Shell: Interpretive Cycle, Shell Offerings, Pattern Matching,
Escaping and Quoting, Redirection, Pipes, Tee, Command Substitution,
Shell Variables, and Essential Shell Programming
The shell is an integral part of Unix/Linux systems, providing both an interactive interface for
users and a programming environment for automating tasks. It is a command-line interpreter
that processes user commands, executes programs, and provides powerful scripting
capabilities. This guide provides a detailed explanation of the core shell concepts, including the
interpretive cycle, pattern matching, escaping and quoting, redirection, pipes, tee, command
substitution, shell variables, and essential shell programming.
1. The Shell’s Interpretive Cycle
The shell follows a cycle for interpreting and executing commands, which can be broken down
into the following steps:
1. Command Input: The user enters a command at the shell prompt.
2. Tokenization: The shell splits the input into tokens, breaking down the command into its
constituent components, such as the command name, arguments, operators, and
special symbols.
3. Expansion: The shell performs various expansions:
○ Variable expansion: Substituting the value of environment variables.
○ Command substitution: Replacing a command with its output.
○ Wildcard expansion: Expanding wildcards like *, ?, and [] to match files.
○ Tilde expansion: Expanding ~ to the user’s home directory.
4. Parsing: The shell checks the syntax of the command to ensure that it is valid and
prepares it for execution.
5. Redirection/Piping: If redirection or pipes are present, the shell sets up the appropriate
file descriptors and connects commands through pipes.
6. Execution: The shell executes the command, either by invoking a program or running a
script.
7. Output/Return: The command produces output (if any), which is printed to the terminal
or redirected to a file. The shell then returns to waiting for input.
2. Shell Offerings
The shell provides a variety of features that allow for efficient interaction with the system. Some
key offerings include:
a. Job Control
● Allows users to manage background and foreground processes.
○ Background (bg): Resume a suspended job in the background.
○ Foreground (fg): Bring a job to the foreground.
○ Jobs (jobs): List the current jobs and their statuses.
b. Alias
● Alias allows you to define custom shortcuts for frequently used commands.
○ Example: alias ll='ls -l' creates an alias ll for ls -l.
c. History
● The shell keeps a history of commands, enabling users to reuse commands with the
history command.
○ Pressing the up arrow allows users to scroll through previous commands.
d. Globbing/Pattern Matching
● The shell supports file matching using wildcards, enabling pattern-based file selection.
e. Shell Functions
You can define functions to group commands for reuse:
bash
my_function() {
echo "Hello, $1"
}
f. Control Structures
● The shell provides structures for decision-making and looping:
○ if, else for conditional execution.
○ for, while, until for looping.
○ case for pattern matching.
3. Pattern Matching in the Shell
Pattern matching (also called globbing) allows you to match files or strings based on patterns.
This enables more flexible file management and searching.
Common Wildcards:
● * (Asterisk): Matches any sequence of characters (including no characters).
○ Example: *.txt matches all .txt files in a directory.
● ? (Question Mark): Matches exactly one character.
○ Example: file?.txt matches file1.txt, fileA.txt, etc.
● [] (Square Brackets): Matches any one character inside the brackets.
○ Example: file[1-3].txt matches file1.txt, file2.txt, and
file3.txt.
● {} (Curly Braces): Used for creating lists of strings.
○ Example: file{1,2,3}.txt matches file1.txt, file2.txt, and
file3.txt.
4. Escaping and Quoting
Escaping and quoting are crucial for controlling how special characters are interpreted by the
shell.
Types of Quoting:
● Single Quotes ('): Prevent the shell from interpreting special characters inside the
quotes. Everything inside single quotes is treated literally.
○ Example: echo '$HOME' prints $HOME, not the value of the variable.
● Double Quotes ("): Allows for variable expansion and command substitution, but
prevents interpretation of certain special characters like spaces.
○ Example: echo "$HOME" prints the value of the HOME environment variable.
● Backslashes (\): Escape individual characters.
○ Example: echo "I\'m a student" prints I'm a student.
5. Redirection in the Shell
Redirection allows you to control where input and output of commands go (either to files or other
streams).
Common Redirection Operators:
● Input Redirection (<): Redirects input to a command from a file.
○ Example: sort < file.txt sorts the contents of file.txt.
● Output Redirection (>): Redirects output to a file (overwrites the file).
○ Example: echo "Hello" > output.txt writes "Hello" to output.txt.
● Append Output (>>): Appends output to a file.
○ Example: echo "Goodbye" >> output.txt appends "Goodbye" to
output.txt.
● Error Redirection (2>): Redirects error output to a file.
○ Example: command 2> error.txt redirects error messages to error.txt.
● Redirecting Both Output and Error (&>): Redirects both standard output and error
output to the same file.
○ Example: command &> output.txt writes both output and errors to
output.txt.
6. Pipes in the Shell
Pipes (|) are used to connect the output of one command to the input of another command.
This allows for creating powerful command pipelines.
Example:
bash
cat file.txt | grep "pattern" # Filters the contents of file.txt to
show only lines containing 'pattern'
Multiple pipes can be chained together:
bash
cat file.txt | sort | uniq # Sorts file.txt and removes duplicate
lines
7. Tee Command
The tee command is used to read from the standard input and write to both standard output
and one or more files. This is useful when you want to view the output of a command and save
it simultaneously.
Syntax:
bash
command | tee file.txt
Example:
bash
echo "Hello, World" | tee output.txt # Writes "Hello, World" to both
the terminal and output.txt
Appending to a File:
You can append to a file using the -a option:
bash
echo "More text" | tee -a output.txt # Appends "More text" to
output.txt
8. Command Substitution
Command substitution allows the output of a command to be used as an argument in another
command.
Syntax:
bash
$(command) # Preferred method (modern)
or
bash
`command` # Older syntax (deprecated but still widely used)
Example:
bash
echo "The current date is $(date)" # Uses the output of the date
command
This is useful for dynamically passing values in commands and scripts.
9. Shell Variables
Variables are used to store data (such as strings, numbers, or command outputs) in the shell.
They can be used to simplify commands and improve script readability.
Setting Variables:
bash
variable_name=value # No spaces around the equal sign
Example:
bash
name="Alice"
echo $name # Prints 'Alice'
Special Variables:
● $HOME: Represents the user’s home directory.
● $USER: Represents the current user’s username.
● $PWD: Represents the current working directory.
● $?: Represents the exit status of the last command.
● $0: Represents the script’s name.
● $$: Represents the process ID of the current shell.
Exporting Variables:
Variables can be exported to become environment variables, which are accessible by
subprocesses.
bash
export PATH=$PATH:/new/directory # Adds a new directory to the PATH
10. Essential Shell Programming
Shell scripting allows you to automate repetitive tasks by writing sequences of commands in a
file.
Key Concepts:
● Control Structures:
If-else Statements:
bash
if [ condition ]; then
command1
else
command2
fi
○
○ Loops:
For loop:
bash
for i in {1..5}; do
echo $i
done
■
While loop:
bash
while [ condition ]; do
command
done
Functions: Functions allow you to group a set of commands and reuse them.
bash
greet() {
echo "Hello, $1"
}
greet "Alice" # Outputs: Hello, Alice
●
● Scripts: Shell scripts are plain text files containing shell commands. They can be
executed by making them executable (chmod +x script.sh) and running them
(./script.sh).