Introduction to Bash Shell
Scripting
By:
Dr. Basharat Mahmood
Dr. Madiha Yousuf
High Impact IT Training, 2024.
What is Bash?
• Definition:
• Bash (Bourne Again SHell) is a Unix shell and
command language.
• History:
• Developed by Brian Fox for the GNU Project as a
free software replacement for the Bourne shell.
• Significance:
• Default shell on many Linux distributions and
macOS.
High Impact Training, 2024.
Importance of Bash Scripting
• Automation:
• Automates repetitive tasks, saving time and
reducing errors.
• Efficiency:
• Streamlines processes, making workflows faster
and more efficient.
• System Administration:
• Essential for managing Linux systems and
servers, performing tasks like backups, updates,
and monitoring.
High Impact Training, 2024.
Basic Rules of Bash Scripting
• File Extensions:
• Scripts typically use .sh extension, e.g., script.sh.
• Shebang:
• Start scripts with #!/bin/bash to specify the script
interpreter.
• Comments:
• Use # to add comments for clarity.
• Example: # This is a comment
• Permissions:
• Make scripts executable using chmod +x script.sh.
High Impact Training, 2024.
Steps to Write and Execute a Bash Script
• Write the Script
• Use a text editor like nano, vim, or gedit to write the script.
• Save the Script
• Make Executable
• Change file permissions to make it executable: chmod +x
script.sh.
• Move the file to some suitable location. For example:
/usr/bin
• Execute the Script
• Run the script by typing script name
High Impact Training, 2024.
Syntax Overview
• Basic Syntax:
• Commands are written as they are executed in the terminal.
• Whitespace:
• Important for readability, although not always syntactically
necessary.
• Semicolons:
• Used to separate multiple commands on a single line.
• Example: echo "Hello"; echo "World"
High Impact Training, 2024.
Input/Output Commands
• echo:
• Prints text to the terminal.
• Example:
echo "Hello, World!"
• read:
• Reads input from the user.
• Example:
read -p "Enter your name: " name
echo "Hello, $name"
• printf:
• Formats and prints text.
• Example:
printf "Name: %s\n" "$name"
High Impact Training, 2024.
Using Linux Commands in Scripts
• Example Commands:
• ls, pwd, cd, cp, mv, rm
• Integration:
• Example:
echo "Current Directory:"
pwd
echo "Listing Files:"
ls
High Impact Training, 2024.
Variables
• Creating Variables:
• Example:
name="John"
age=25
• Using Variables:
• Example:
echo "Name: $name"
echo "Age: $age"
High Impact Training, 2024.
Arithmetic Operators
• Using expr:
• Example:
result=$(expr 5 + 3)
echo $result
• Using let:
• Example:
let result=5+3
echo $result
• Using $(( )):
• Example:
result=$((5 + 3))
echo $result
High Impact Training, 2024.
Logical Operators
• AND (&&):
• Example:
if [ $a -lt 10 ] && [ $b -gt 5 ]; then
echo "True"
fi
• OR (||):
• Example:
if [ $a -lt 10 ] || [ $b -gt 5 ]; then
echo "True"
fi
• NOT (!):
• Example:
if ! [ $a -lt 10 ]; then
echo "False"
fi
High Impact Training, 2024.
Comparison Operators
• Equality (-eq):
• Example:
if [ $a -eq $b ]; then
echo "Equal"
fi
• Not equal (-ne):
• Example:
if [ $a -ne $b ]; then
echo "Not Equal"
fi
• Less than (-lt):
• Example:
if [ $a -lt $b ]; then
echo "Less than"
fi
High Impact Training, 2024.
Comparison Operators
• Greater than (-gt):
• Example:
if [ $a -gt $b ]; then
echo "Greater than"
fi
• Less than or equal (-le):
• Example:
if [ $a -le $b ]; then
echo "Less than or equal"
fi
• Greater than or equal (-ge):
• Example:
if [ $a -ge $b ]; then
echo "Greater than or equal"
fi
High Impact Training, 2024.
File Operators
• Exists (-e):
• Example:
if [ -e $file ]; then
echo "File exists"
fi
• Regular file (-f):
• Example:
if [ -f $file ]; then
echo "Regular file"
fi
• Directory (-d):
• Example:
if [ -d $file ]; then
echo "Directory"
fi
High Impact Training, 2024.
File Operators
• Readable (-r):
• Example:
if [ -r $file ]; then
echo "Readable"
fi
• Writable (-w):
• Example:
if [ -w $file ]; then
echo "Writable"
fi
• Executable (-x):
• Example:
if [ -x $file ]; then
echo "Executable"
fi
High Impact Training, 2024.
Conditional Statements: if-else
• Syntax:
if [ condition ]; then
# statements
else
# statements
fi
• Example:
if [ $age -ge 18 ]; then
echo "Adult"
else
echo "Minor"
fi
High Impact Training, 2024.
Conditional Statements: elif
• Syntax:
• if [ condition1 ]; then
# statements
elif [ condition2 ]; then
# statements
else
# statements
fi
• Example:
if [ $marks -ge 90 ]; then
echo "Grade A"
elif [ $marks -ge 75 ]; then
echo "Grade B"
else
echo "Grade C"
fi
High Impact Training, 2024.
Conditional Statements: case
• Syntax:
case $variable in
pattern1)
# statements
;;
pattern2)
# statements
;;
*)
# default statements
;;
esac
High Impact Training, 2024.
Conditional Statements: case
• Example:
case $day in
"Monday")
echo "Start of the work week"
;;
"Friday")
echo "End of the work week"
;;
*)
echo "Midweek"
;;
esac
High Impact Training, 2024.
Looping Statements: for loop
• Syntax:
• Example:
for i in {1..10}; do
echo $i
done
• Example:
for file in *.txt; do
echo "Processing $file"
done
High Impact Training, 2024.
Looping Statements: while loop
• Syntax:
while [ condition ]; do
# statements
done
• Example:
count=1
while [ $count -le 5 ]; do
echo $count
((count++))
done
High Impact Training, 2024.
Looping Statements: until loop
• Syntax:
until [ condition ]; do
# statements
done
• Example:
count=1
until [ $count -gt 5 ]; then
echo $count
((count++))
Done
High Impact Training, 2024.
Loop Control: break and continue
• break:
Example:
for i in {1..10}; do
if [ $i -eq 5 ]; then
break
fi
echo $i
done
• continue:
Example:
for i in {1..10}; do
if [ $i -eq 5 ]; then
continue
fi
echo $i
done
High Impact Training, 2024.
Arrays
• Creating Arrays:
• Example:
array=(1 2 3 4 5)
• Accessing Elements:
• Example:
echo ${array[0]}
• Iterating:
• Example:
for i in "${array[@]}"; do
echo $i
done
High Impact Training, 2024.
Functions
• Defining Functions:
• Example:
function greet() {
echo "Hello, $1"
}
• Calling Functions:
• Example:
greet "John"
High Impact Training, 2024.
Input/Output Redirection
• Redirecting Output:
• Example:
command > file.txt
• Appending Output:
• Example:
command >> file.txt
• Redirecting Input:
• Example:
command < file.txt
• Piping:
• Example:
command1 | command2
High Impact Training, 2024.
Script Arguments
• Accessing Arguments:
• Example:
echo $1 $2
• Special Variables:
• $#: Number of arguments
• $@: All arguments
• $*: All arguments as a single string
• Example:
echo "Number of arguments: $#"
echo "All arguments: $@"
High Impact Training, 2024.
String Manipulation
• Concatenation:
• Example:
str="Hello"
str+=" World"
echo $str
• Slicing:
• Example:
echo ${str:0:5}
• Length:
• Example:
echo ${#str}
High Impact Training, 2024.
Environment Variables
• Common Variables:
• PATH, HOME, USER, SHELL
• Using Environment Variables:
• Example:
echo $HOME
High Impact Training, 2024.
The end
High Impact Training, 2024.