MODULE 2
Shell Responsibilities
The shell is a command-line interpreter that acts as an interface between the user and the
operating system kernel. Its main responsibility is to read user commands, interpret
them, and execute the appropriate programs. In addition to command execution, the shell
performs several important functions that help users interact efficiently with the system.
Responsibilities of the Shell
1. Command Interpretation
● The shell reads commands entered by the user.
● It interprets the command, arguments, and options.
● Determines whether the command is a built-in command or an external program.
This is the primary responsibility of the shell.
2. Program Execution
● The shell starts the execution of programs requested by the user.
● It creates child processes to run commands.
● Communicates with the kernel to execute programs.
3. Input and Output Redirection
The shell manages input, output, and error redirection, allowing data to be read from files or
written to files instead of the terminal. Operators like <, >, and >> are handled by the shell
before command execution.
4. Pipeline Handling
● The shell supports pipes (|).
● Connects the output of one command to the input of another.
● Helps combine multiple commands for complex tasks.
5. Environment Management
● The shell maintains environment variables.
● Passes environment information to programs during execution.
● Allows users to define and modify variables.
6. Job Control
● The shell manages background and foreground processes.
● Allows users to suspend, resume, or terminate jobs.
● Commands like bg, fg, and jobs are supported.
7. Filename Expansion (Globbing)
● The shell expands wildcard characters such as *, ?, and [].
● Helps match multiple files with a single pattern.
Example:
ls *.txt
8. Command History Management
● Stores previously executed commands.
● Allows users to recall and reuse commands.
● Improves productivity and reduces typing errors.
9. Error Handling and Reporting
● Displays error messages for invalid commands.
● Reports syntax errors and execution failures.
● Helps users identify and correct mistakes.
Pipes (in UNIX / Linux Shell)
A pipe is a mechanism used in the shell to allow communication between two or more
commands. It connects the output of one command directly to the input of another
command. Pipes enable users to combine simple commands to perform complex operations
efficiently.
The pipe operator is represented by the symbol |.
Syntax of Pipes
command1 | command2
● command1 produces output
● command2 consumes that output as input
Working of Pipes
● The shell creates a pipeline between commands
● Output of the first command flows into the second command
● Commands run concurrently
● Data is transferred through memory buffers managed by the kernel
Examples of Pipes
ls | wc -l
Counts the number of files in the current directory.
In this example:
• ls lists files
• wc -l counts the number of lines
• The output of ls becomes the input of wc -l
Advantages of Pipes
● Simplifies command execution
● Avoids temporary files
● Saves disk space
● Increases efficiency
● Encourages modular command usage
Input Redirection and Output Redirection
(Shell)
In UNIX/Linux, redirection allows the user to change the standard input (stdin) or standard
output (stdout) of a command. Instead of using the keyboard or screen, input and output can
be redirected to or from files.
Input Redirection
Meaning
Input redirection allows a command to take its input from a file instead of the keyboard. The
shell redirects the contents of a file to the standard input of a command.
Symbol Used
<
Syntax
command < inputfile
Working
● The shell opens the specified file
● The file’s contents are given as input to the command
● The command reads input as if it were typed from the keyboard
Examples
cat < [Link]
Displays the contents of [Link].
sort < [Link]
Sorts the contents of [Link].
Output Redirection
Meaning
Output redirection sends the output of a command to a file instead of the screen. This is
useful for saving command results.
1. Output Redirection (Overwrite)
Symbol Used
>
Syntax
command > outputfile
Working
● If the file exists, its contents are overwritten
● If the file does not exist, it is created
Example
ls > [Link]
Stores the list of files in [Link].
2. Output Redirection (Append)
Symbol Used
>>
Syntax
command >> outputfile
Working
● Output is added to the end of the file
● Existing contents are not removed
Example
echo "Hello" >> [Link]
Appends “Hello” to [Link].
Input and Output Redirection Together
● Both input and output can be redirected in a single command
sort < [Link] > [Link]
This sorts the contents of [Link] and stores the result in [Link].
Standard Streams Involved
● stdin (0) – standard input (keyboard)
● stdout (1) – standard output (screen)
● stderr (2) – standard error
Redirection mainly affects stdin and stdout.
Advantages of Redirection
● Saves command output to files
● Avoids manual typing of input
● Useful in automation and scripting
● Improves flexibility of shell usage
Conclusion (Exam Line)
Input redirection allows commands to read input from files instead of the keyboard, while output
redirection allows command output to be written to files instead of the screen. Redirection
makes shell commands more flexible and powerful.
Here Documents (in Shell)
A here document is a type of input redirection in UNIX/Linux shell that allows a command to
take multiple lines of input directly from the shell script or command line, instead of from a
file or keyboard. The input is provided inline until a specified delimiter is reached.
Here documents are commonly used in shell scripts to supply predefined input to commands.
Symbol Used
<<
This operator tells the shell to read input until the delimiter is found.
Syntax of Here Document
command << DELIMITER
input line 1
input line 2
input line 3
DELIMITER
● DELIMITER can be any word (commonly EOF)
● The delimiter must appear alone on a line
Working of Here Document
● The shell reads input lines after <<
● Input continues until the delimiter is encountered
● All input lines are passed to the command as standard input
● The delimiter line itself is not included in the input
Examples of Here Documents
cat << EOF
This is line one
This is line two
This is line three
EOF
This prints the given lines to the screen.
wc -l << END
Apple
Banana
Mango
END
This counts the number of lines provided.
Advantages of Here Documents
● Simple way to provide multi-line input
● Improves readability of scripts
● Avoids use of temporary files
● Useful in automation
Running a Shell Script
A shell script is a text file that contains a sequence of shell commands. Running a shell script
means executing these commands automatically by the shell instead of typing them one by one.
In Linux, a shell script is usually executed using the bash shell.
Creating a Shell Script (Basic Step)
● A shell script is created using any text editor.
● The first line usually contains the shebang to specify the shell.
Example:
#!/bin/bash
echo "Hello World"
The #!/bin/bash line tells the system to use the bash shell to run the script.
Methods of Running a Shell Script
There are three common ways to run a shell script in Linux.
1. Running the Script Using the Shell Command
● The script is executed by explicitly calling the shell.
● Execution permission is not required.
Syntax:
bash [Link]
Example:
bash [Link]
This method is simple and commonly used for testing scripts.
2. Running the Script Using Source (Dot Command)
● The script is executed in the current shell environment.
● Any variables defined in the script remain available after execution.
Syntax:
. [Link]
or
source [Link]
This method is mainly used when the script sets environment variables.
3. Running the Script as an Executable File
This is the most common and standard method.
Steps Involved:
Step 1: Give execute permission
chmod +x [Link]
Step 2: Run the script
./[Link]
● The ./ specifies the current directory.
● The script must contain the shebang line.
Importance of Execute Permission
● Linux treats scripts as files, not commands
● Without execute permission, the script cannot be run directly
● chmod is used to change file permissions
The Shell as a Programming Language
The shell is not only a command interpreter but also a powerful programming language. It
allows users to write programs called shell scripts, which consist of a sequence of commands
along with programming constructs such as variables, control structures, and functions. Using
the shell as a programming language helps automate tasks and perform complex operations
efficiently.
Why the Shell is Considered a Programming Language
● It supports variables and data storage
● It provides control structures like conditionals and loops
● It allows decision making
● It supports functions
● It enables input/output handling
● It allows reuse and automation of commands
Features of Shell as a Programming Language
1. Variables
● Shell supports user-defined variables
● Variables store data temporarily
● No data type declaration is required
Example:
count=10
2. Input and Output
● Shell scripts can take input from users or files
● Output can be displayed or redirected to files
Example:
echo "Enter name:"
read name
3. Conditional Statements
● Used for decision making
● Common conditional constructs:
○ if
○ if-else
○ case
Example:
if [ $x -gt 10 ]
then
echo "Greater than 10"
fi
4. Looping Statements
● Used to repeat commands
● Types of loops:
○ for
○ while
○ until
Example:
for i in 1 2 3
do
echo $i
done
5. Functions
● Shell allows grouping of commands into functions
● Improves modularity and reusability
Example:
myfunc() {
echo "Hello"
}
6. Command Substitution
● Allows output of a command to be used as input
Example:
files=$(ls)
[Link] Operations
The shell allows arithmetic calculations using operators like +, -, *, and /. Arithmetic expressions
can be evaluated using $(( )).
Example:
sum=$((a + b))
Shell Meta Characters
Shell meta characters are special characters that have a predefined meaning in the shell.
They are interpreted by the shell before a command is executed and are used to perform
tasks such as filename expansion, redirection, piping, variable substitution, and command
control.
Meta characters make the shell powerful and flexible.
Common Shell Meta Characters and Their Uses
1. * (Asterisk)
● Matches zero or more characters in filenames
● Used for filename expansion (globbing)
Example:
ls *.txt
Lists all files ending with .txt.
2. ? (Question Mark)
● Matches exactly one character
Example:
ls file?.txt
Matches [Link], [Link] but not [Link].
3. [ ] (Character Class)
● Matches any one character from a given set
Example:
ls file[123].txt
Matches [Link], [Link], [Link].
4. | (Pipe)
● Connects the output of one command to the input of another command
Example:
ls | wc -l
Counts number of files.
5. < (Input Redirection)
● Redirects input from a file instead of keyboard
Example:
sort < [Link]
6. > (Output Redirection)
● Redirects output to a file (overwrites file)
Example:
ls > [Link]
7. >> (Append Redirection)
● Appends output to an existing file
Example:
echo "Hello" >> [Link]
8. ; (Command Separator)
● Allows multiple commands on the same line
● Commands execute sequentially
Example:
date; who
9. & (Background Execution)
● Runs a command in the background
Example:
sleep 10 &
10. $ (Variable Expansion)
● Used to access the value of a variable
Example:
echo $USER
11. \ (Escape Character)
● Removes special meaning of the next character
Example:
echo \$USER
Prints $USER literally.
12. # (Comment)
● Anything after # is treated as a comment
Example:
# This is a comment
File Name Substitution (Shell)
File name substitution is a feature of the UNIX/Linux shell in which special characters
(wildcards) are used to represent one or more filenames. Before executing a command, the
shell replaces these patterns with the matching filenames present in the current directory.
This process is also known as globbing.
File name substitution is done by the shell, not by the command itself.
Purpose of File Name Substitution
● To handle multiple files easily
● To reduce typing effort
● To make commands more flexible and powerful
Wildcards Used in File Name Substitution
1. * (Asterisk)
● Matches zero or more characters
● Most commonly used wildcard
Example:
ls *.txt
Lists all files ending with .txt.
rm file*
Deletes all files starting with file.
2. ? (Question Mark)
● Matches exactly one character
Example:
ls file?.txt
Matches [Link] and [Link], but not [Link].
3. [ ] (Character Class)
● Matches any one character from a specified set or range
Example:
ls file[123].txt
Matches [Link], [Link], [Link].
ls file[a-z].txt
Matches filenames with a single lowercase letter.
Order of Substitution
● The shell performs file name substitution before executing the command
● If no matching file is found:
○ The pattern may be passed as it is, or
○ The command may return an error (depending on shell settings)
Example Showing Substitution
Command:
echo *.c
If files a.c, b.c, and test.c exist, the shell converts the command to:
echo a.c b.c test.c
Shell Variables
Shell variables are used to store data temporarily in the shell. They allow the shell and shell
scripts to store values such as numbers, strings, and command outputs, which can be reused
during execution. Shell variables make scripts flexible and dynamic.
Shell variables do not require data type declaration.
Defining Shell Variables
A shell variable is defined using the assignment operator = without spaces.
Example:
name="Linux"
count=10
Accessing Variable Values
The value of a variable is accessed using the $ symbol.
Example:
echo $name
Rules for Shell Variables
• Variable names are case-sensitive
• No spaces are allowed around =
• Variable names can contain letters, numbers, and underscore
• Variables usually hold string values by default
Types of Shell Variables
1. User-Defined Variables
● Created and used by the user
● Valid only within the shell or script where they are defined
Syntax:
variable=value
Example:
count=10
name="Linux"
Accessing variable:
echo $count
2. Environment Variables
● Available to the shell and all child processes
● Used to control the environment of programs
Common environment variables:
● HOME – user home directory
● PATH – command search path
● USER – current user name
● SHELL – default shell
Display:
echo $HOME
Read-Only Variables
● Cannot be modified once set
readonly pi=3.14
Unsetting Variables
● Removes a variable from the shell
unset variable_name
Command Substitution
Command substitution is a feature of the UNIX/Linux shell that allows the output of a
command to be used as input to another command or stored in a variable. Before
executing the command line, the shell runs the command inside the substitution and replaces it
with its output.
This makes shell commands and scripts more powerful and flexible.
Purpose of Command Substitution
● To reuse the output of one command
● To store command output in variables
● To combine commands dynamically
● To simplify shell scripts
Methods of Command Substitution
There are two ways to perform command substitution in the shell.
1. Using Back Quotes `command`
● The older method of command substitution
● The command is enclosed within back quotes
Syntax:
`command`
Example:
today=`date`
echo $today
Limitation:
● Difficult to read
● Hard to nest commands
2. Using $() (Preferred Method)
● Modern and recommended method
● Easier to read and nest
● Supported by bash and POSIX shells
Syntax:
$(command)
Example:
today=$(date)
echo $today
How Command Substitution Works
● The shell executes the command inside $() or back quotes
● Captures its standard output
● Replaces the substitution with that output
● The final command is then executed
Examples of Command Substitution
files=$(ls)
echo $files
count=$(ls | wc -l)
echo "Number of files: $count"
echo "Current directory is $(pwd)"
Using Command Substitution in Scripts
● Frequently used to assign dynamic values
● Useful in automation and system scripts
Example:
#!/bin/bash
user=$(whoami)
echo "Logged in user: $user"
Environment (Shell Environment)
The shell environment refers to a set of variables and settings that control the behavior of the
shell and the programs executed from it. These variables store information such as user details,
system paths, and configuration options. The environment is created when a user logs into the
system and is inherited by child processes.
Environment Variables
Environment variables are special variables that are available to the shell and all programs
started from it. They define important system properties and influence program execution.
Examples:
PATH
HOME
USER
SHELL
Viewing Environment Variables
Environment variables can be displayed using:
env
printenv
echo $PATH
Creating and Exporting Environment Variables
A variable becomes an environment variable when it is exported.
Example:
course="OS"
export course
Now, course is available to child processes.
Modifying Environment Variables
Environment variables can be modified temporarily or permanently.
Example:
export PATH=$PATH:/new/path
Commonly Used Environment Variables
• PATH – Specifies command search path
• HOME – User’s home directory
• USER – Logged-in user name
• PWD – Present working directory
Control Structures in Shell
Control structures are used in shell scripting to control the flow of execution of commands.
They allow the shell to make decisions, repeat commands, and select actions based on
conditions. Control structures make shell scripts powerful and flexible.
Types of Control Structures
Shell control structures are broadly classified into:
● Conditional statements
● Looping statements
● Selection statements
1. Conditional Statements
Conditional statements are used for decision making.
a) if Statement
● Executes commands if a condition is true.
Syntax:
if condition
then
commands
fi
Example:
if [ $a -gt $b ]
then
echo "a is greater"
fi
b) if-else Statement
● Provides an alternative block if the condition is false.
Syntax:
if condition
then
commands
else
commands
fi
if [ $a -eq $b ]; then
echo "Equal"
else
echo "Not equal"
fi
c) if-elif-else Statement
● Used to test multiple conditions.
Syntax:
if condition1
then
commands
elif condition2
then
commands
else
commands
fi
2. Selection Statement (case)
The case statement is used when multiple conditions depend on the value of a variable.
Syntax:
case value in
pattern1) commands ;;
pattern2) commands ;;
*) default commands ;;
esac
Example:
case $day in
Mon) echo "Monday" ;;
Tue) echo "Tuesday" ;;
*) echo "Invalid day" ;;
esac
3. Looping Statements
Loops are used to repeat commands.
a) for Loop
● Executes commands for a list of values.
Syntax:
for var in list
do
commands
done
Example:
for i in 1 2 3
do
echo $i
done
b) while Loop
● Executes commands while a condition is true.
Syntax:
while condition
do
commands
done
while [ $n -gt 0 ]
do
echo $n
n=$((n-1))
done
c) until Loop
● Executes commands until a condition becomes true.
Syntax:
until condition
do
commands
done
Loop Control Statements
break
● Terminates the loop immediately.
continue
● Skips the remaining commands and continues with the next iteration.
Arithmetic in Shell
Arithmetic in shell scripting refers to performing mathematical operations such as addition,
subtraction, multiplication, and division within the shell. Since the shell primarily treats variables
as strings, special syntax and commands are used to handle arithmetic operations.
Shell arithmetic mainly supports integer operations.
Methods of Performing Arithmetic in Shell
There are four common methods to perform arithmetic in shell scripts.
1. Using expr Command
● expr is a command used to evaluate arithmetic expressions.
● Spaces are mandatory between operands and operators.
● Special characters like * must be escaped.
Syntax:
expr expression
Example:
a=10
b=5
sum=`expr $a + $b`
echo $sum
2. Using $(( )) Arithmetic Expansion (Preferred)
● Modern and recommended method.
● Easier to read and use.
● No need to escape operators.
Syntax:
result=$((expression))
Example:
a=10
b=5
sum=$((a + b))
echo $sum
3. Using let Command
● Evaluates arithmetic expressions.
● No $ is required before variable names.
Example:
let sum=a+b
echo $sum
4. Using Square Brackets with (( ))
● Used mainly in conditional expressions.
● Returns exit status instead of value.
Example:
((a > b))
Arithmetic Operators in Shell
Arithmetic Operators
● + Addition
● - Subtraction
● * Multiplication
● / Division
● % Modulus
Relational Operators
● -eq equal
● -ne not equal
● -gt greater than
● -lt less than
● -ge greater than or equal
● -le less than or equal
Example Program: Arithmetic in Shell
#!/bin/bash
a=20
b=10
echo "Addition: $((a + b))"
echo "Subtraction: $((a - b))"
echo "Multiplication: $((a * b))"
echo "Division: $((a / b))"
Interrupt Processing
Interrupt processing is a mechanism by which the CPU responds to events that require
immediate attention. An interrupt temporarily stops the execution of the currently running
program and transfers control to a special routine called an Interrupt Service Routine (ISR).
After the interrupt is handled, the CPU resumes the normal execution.
Interrupts are essential for efficient CPU utilization and responsive operating system behavior.
What is an Interrupt?
● An interrupt is a signal sent to the CPU
● It indicates that an event has occurred that needs immediate handling
● Interrupts can occur due to hardware or software events
Types of Interrupts
1. Hardware Interrupts
● Generated by external hardware devices
● Examples:
○ Keyboard input
○ Mouse movement
○ Disk I/O completion
● Allow devices to notify the CPU when they need service
2. Software Interrupts
● Generated by software instructions
● Examples:
○ System calls
○ Exceptions (divide by zero)
● Used by programs to request services from the operating system
Interrupt Processing Steps
1. Interrupt Generation
● A device or software generates an interrupt signal
● The signal is sent to the CPU
2. Interrupt Acknowledgement
● The CPU completes the current instruction
● Interrupts are temporarily disabled
● CPU acknowledges the interrupt request
3. Saving CPU Context
● The CPU saves the current state:
○ Program counter
○ Registers
○ Status information
● This allows the interrupted program to resume later
4. Interrupt Vector Lookup
● The CPU identifies the type of interrupt
● Uses the interrupt vector table to find the address of the ISR
5. Execution of Interrupt Service Routine (ISR)
● Control is transferred to the ISR
● The ISR performs the required service
● Example: reading data from an I/O device
6. Restoring CPU Context
● After ISR execution, the saved CPU state is restored
● Interrupts are re-enabled
7. Resume Normal Execution
● CPU resumes execution of the interrupted program
● Execution continues as if the interrupt never occurred
Interrupt Vector Table
● A data structure maintained by the operating system
● Contains addresses of ISRs
● Allows fast identification of the correct ISR
Importance of Interrupt Processing
● Improves CPU efficiency
● Eliminates busy waiting
● Enables multitasking
● Allows fast response to external events
● Essential for I/O operations
Functions in Shell
A function in shell scripting is a named block of commands that performs a specific task.
Functions help in organizing code, avoiding repetition, and improving readability of shell
scripts. Once defined, a function can be called multiple times within a script.
Why Functions are Used
● To reuse code
● To divide a large script into smaller parts
● To improve readability and maintenance
● To reduce redundancy
● To make scripts modular
Defining a Function
A function can be defined in the shell in two common ways.
Syntax 1
function_name() {
commands
}
Syntax 2
function function_name {
commands
}
Calling a Function
● A function is called by writing its name
● No parentheses are used while calling
Example:
function_name
Example of a Simple Shell Function
#!/bin/bash
greet() {
echo "Hello, Welcome to Shell Programming"
}
greet
Passing Arguments to Functions
● Functions can accept arguments
● Arguments are accessed using positional parameters:
○ $1, $2, $3, …
Example:
add() {
sum=$(( $1 + $2 ))
echo "Sum = $sum"
}
add 10 20
Return Values from Functions
● Shell functions return values using the exit status
● The return statement is used
● Return value range: 0–255
Example:
check() {
return 0
}
check
echo $?
Local Variables in Functions
● Variables declared inside functions are global by default
● local keyword is used to make variables local
Example:
myfunc() {
local x=10
echo $x
}
Function Scope
● Functions can access:
○ Global variables
○ Environment variables
● Local variables are accessible only inside the function
Advantages of Using Functions
● Improves code reusability
● Simplifies debugging
● Makes scripts structured
● Easy modification and updates