Before we proceed learning Bash scripting, it’s essential to first grasp the foundational concepts in order to understand why Bash scripting is so powerful.
Foundational Concepts
1️⃣ File Permissions & Execution
• Why? You need to ensure your scripts have the right permissions to execute.
Concepts to Learn:
• chmod +x script.sh → Makes a script executable.
• chown user:group script.sh → Changes ownership.
• ls -l script.sh → Checks file permissions.
2️⃣ Variables & Data Types
• Why? Store and reuse values within scripts.
Concepts to Learn:
• Defining variables: name="Abba"
• Accessing variables: echo $name
• Environment variables: $HOME, $PATH
• Read-only variables: readonly varName
• Unset variables: unset varName
📌 Example:
#!/bin/bash
name="Alice"
echo "Hello, $name!"
3️⃣ User Input & Arguments
• Why? Make your scripts interactive.
Concepts to Learn:
• read → Read user input.
• $1, $2, $@, $# → Command-line arguments.
📌 Example:
#!/bin/bash
echo "Enter your name:"
read user_name
echo "Welcome, $user_name!"
📌 Using Arguments:
#!/bin/bash
echo "Script name: $0"
echo "First argument: $1"
echo "Second argument: $2"
Run it as:
./script.sh Hello World
4️⃣ Conditional Statements (if-else, case)
• Why? Make decisions based on conditions.
Concepts to Learn:
• [ ] or [[ ]] → Conditions
• if-else statements.
• case statements.
📌 Example:
#!/bin/bash
echo "Enter a number:"
read num
if (( num % 2 == 0 )); then
echo "Even"
else
echo "Odd"
fi
5️⃣ Loops (for, while, until)
• Why? Automate repetitive tasks.
Concepts to Learn:
• for loop → Iterate over a range or list.
• while loop → Run until a condition is false.
• until loop → Run until a condition becomes true.
📌 Example:
#!/bin/bash
for i in {1..5}; do
echo "Loop iteration: $i"
done
📌 While Loop Example:
#!/bin/bash
count=1
while [ $count -le 5 ]; do
echo "Count: $count"
((count++))
done
6️⃣ Functions
• Why? Reuse code and make scripts modular.
Concepts to Learn:
• Defining functions.
• Calling functions.
• Passing parameters to functions.
📌 Example:
#!/bin/bash
greet() {
echo "Hello, $1!"
}
greet "Alice"
7️⃣ File Handling
• Why? Work with files and directories efficiently.
Concepts to Learn:
• Creating files (touch).
• Reading files (cat, less, head, tail).
• Copying/moving/deleting files (cp, mv, rm).
• Checking file existence (-e, -f, -d).
📌 Example:
#!/bin/bash
if [ -f "test.txt" ]; then
echo "File exists"
else
echo "File does not exist"
fi
8️⃣ Process Management
• Why? Manage running processes efficiently.
Concepts to Learn:
• ps → List processes.
• kill → Stop a process.
• nohup → Run a process in the background.
• & → Run in the background.
📌 Example:
#!/bin/bash
echo "Starting script..."
sleep 10 &
echo "Script running in the background!"
9️⃣ Debugging & Error Handling
• Why? Identify and fix issues in scripts.
Concepts to Learn:
• set -x → Debugging mode.
• set -e → Exit on error.
• Redirecting errors: 2>/dev/null
• Using trap to catch signals.
📌 Example:
#!/bin/bash
set -x # Enable debugging
echo "Debugging mode on"
🔟 Automating Tasks with Cron Jobs
• Why? Schedule scripts to run at specific times.
Concepts to Learn:
• crontab -e → Edit cron jobs.
• * command → Schedule format.
📌 Example: Run backup every midnight.
0 0 * * * /home/user/backup.sh
Advanced Concepts
🔹 Pipelines (|) & Redirection (>, >>)
🔹 Arrays & Associative Arrays
🔹 Regular Expressions (grep, sed, awk)
🔹 Networking Commands (ping, curl, wget)
🔹 Logging (tee, logger)
What is Kernel?
Kernel is the heart of the Operating System you are using. It is a software that sits between your system's hardware and the rest of your OS and software.
User software like browser and your favorite game doesn't talk directly to your hardware, instead the Kernel serves as intermediary. One of the main functions of a Kernel is to abstract away the physical differences between Hardware setups.
What is Shell?
In simple words, shell is a command-line interface for interacting with the OS. This is where we can execute commands, run scripts, and manage system processes. It acts as a bridge between the user and the operating system, allowing us to communicate with and control the system through text-based input.
Getting Started with Bash Scripting
- Create a script file:
touch 100DaysOfCloudDevOps.sh
nano 100DaysOfCloudDevOps.sh
• Add the shebang:
#!/bin/bash
Basic Scripts
- Hello World Script
#!/bin/bash
echo "Hello, World!"
Run:
chmod +x hello.sh
./hello.sh
- User Input Script
#!/bin/bash
echo "Enter your name:"
read name
echo "Hello, $name!"
- File Existence Check
#!/bin/bash
echo "Enter filename:"
read file
if [ -e "$file" ]; then
echo "File exists"
else
echo "File does not exist"
fi
- Basic Arithmetic Operations
#!/bin/bash
echo "Enter two numbers:"
read a b
echo "Sum: $((a + b))"
- Directory Listing
#!/bin/bash
echo "Listing all files and directories in the current directory:"
ls
- Simple Conditional Script
#!/bin/bash
echo "Enter a number:"
read num
if (( num % 2 == 0 )); then
echo "Even"
else
echo "Odd"
fi
- Environment Information Script
#!/bin/bash
echo "User: $USER"
echo "Hostname: $HOSTNAME"
echo "OS: $(uname -o)"
- String Length
#!/bin/bash
echo "Enter a string:"
read str
echo "Length: ${#str}"
- Automating with Loops - Create Multiple Directories
#!/bin/bash
echo "Enter directory name prefix:"
read name
echo "Enter start number:"
read start
echo "Enter end number:"
read end
for i in $(seq $start $end); do
mkdir "${name}${i}"
done
Run:
./createDirectories.sh Day 1 90
- Backup Script
#!/bin/bash
echo "Enter the file to backup:"
read filename
echo "Enter the backup directory:"
read backup_dir
cp "$filename" "$backup_dir"
echo "File $filename backed up to $backup_dir"
Automate with Cron:
crontab -e
Add this line to run every day at midnight:
0 0 * * * /path/to/backup.sh
- User Management: Create Users
sudo useradd user1
sudo useradd user2
- User Management: List Users
cut -d: -f1 /etc/passwd
- Password Generator
#!/bin/bash
echo "Enter the length of the password:"
read length
# Generate a random password containing letters, numbers, and special characters
password=$(tr -dc 'A-Za-z0-9_@#$%^&*' </dev/urandom | head -c "$length")
echo "Generated password: $password"
- System Monitoring Scripts - CPU & Memory Usage Monitor
Create a script that prints CPU and memory usage every 5 seconds.
#!/bin/bash
while true; do
echo "CPU Usage: $(top -bn1 | grep "Cpu(s)" | awk '{print $2}')%"
echo "Memory Usage: $(free -m | awk 'NR==2{printf "Memory Usage: %s/%sMB (%.2f%%)\n", $3, $2, $3*100/$2 }')"
sleep 5
done
- Log File Management - Find Large Files in a Directory
Create a script to list files larger than 100MB in a given directory.
#!/bin/bash
echo "Enter directory path:"
read dir
find "$dir" -type f -size +100M
Test: Run it in /var/log or /home to check large files.
- Clear Old Log Files
Delete log files older than 7 days in /var/log. (⚠️ Be careful when deleting files)
#!/bin/bash
find /var/log -name "*.log" -type f -mtime +7 -exec rm -f {} \;
echo "Old log files deleted."
Test: Run and check ls -lh /var/log before & after execution.
- User & Permission Management - Check Active Users
#!/bin/bash
who
- User & Permission Management - Create Multiple Users
#!/bin/bash
for i in user1 user2 user3; do
sudo useradd "$i"
echo "$i:password123" | sudo chpasswd
done
echo "Users created successfully!"
Test: Run and check cat /etc/passwd | grep user.
- Process Management - Kill a Process by Name
Create a script that kills a process by its name.
#!/bin/bash
echo "Enter process name:"
read pname
pkill -f "$pname" && echo "$pname process killed!"
Test: Run a process (ping google.com) and try killing it.
- File Handling Automation - Rename All .txt Files to .bak
#!/bin/bash
for file in *.txt; do
mv "$file" "${file%.txt}.bak"
done
Test: Create .txt files and run the script.
- File Handling Automation - Find and Replace Text in a File
#!/bin/bash
echo "Enter filename:"
read file
echo "Enter text to replace:"
read old
echo "Enter new text:"
read new
sed -i "s/$old/$new/g" "$file"
echo "Text replaced!"
- Networking - Check Internet Connectivity
#!/bin/bash
ping -c 3 google.com && echo "Internet is up!" || echo "No internet connection!"
Test: Run with and without an internet connection.
- Networking - IP Address Information
#!/bin/bash
ip a | grep inet
Test: Run and check system IPs.
This article covers key Bash scripting concepts, including file permissions, variables, user input, conditionals, loops, functions, and process management.
Without Bash scripting, automating tasks, managing files, and handling system processes would be much more time-consuming and error-prone, requiring manual intervention for repetitive tasks. Tasks like user management, system monitoring, and network checks would need to be done individually, making the system less efficient and harder to maintain.
Now it’s time for you to try it too!
Women in Tech’s contributions
Bash, created in 1989 by Brian Fox for the GNU Project, has been primarily developed and maintained by him and later by Chet Ramey.
However, women have made significant contributions to the broader Unix and Linux ecosystems. Notable role models include:
The ENIAC Programmers: During World War II, six women—Kathleen McNulty Mauchly Antonelli, Jean Jennings Bartik, Frances Snyder Holberton, Marlyn Wescoff Meltzer, Frances Bilas Spence, and Ruth Lichterman Teitelbaum—were responsible for programming the ENIAC, one of the earliest electronic general-purpose computers.
Grace Hopper: Her philosophy—that programming should be accessible and understandable—laid the groundwork for later scripting languages like Bash. Before her work, programming was done in complex machine language or assembly code, making it difficult for people without deep technical expertise to automate tasks. Also, the concept of a compiler, which Hopper helped pioneer, is similar to how Bash interprets and executes commands in real time. And Hopper’s idea that a programming language should resemble human language influenced the development of shell scripting, which uses readable commands like if, for, and echo.
Adele Goldberg and Smalltalk: In the 1970s, computer scientist Adele Goldberg was a key figure in the development of Smalltalk-80, an object-oriented programming language that introduced the concept of overlapping windows on graphic display screens. This innovation greatly influenced modern graphical user interfaces and programming environments, including those used for shell development and scripting. Goldberg’s work emphasized making computing more accessible and interactive, principles that resonate in the design of user-friendly scripting languages.
Mary Allen Wilkes and Early Operating Systems: Mary Allen Wilkes, in the 1960s, worked on the LINC computer, considered by many as the first personal computer. She designed its operating system and developed early applications, making computing more personal and interactive. Her efforts in creating user-friendly computing environments laid the groundwork for the development of accessible operating systems and shells, including Unix and its successors.
Mary Ann Horton: A key contributor to Berkeley Unix, Horton worked on the vi editor and Usenet, both crucial tools for Unix users.
Lorinda Cherry: A researcher at Bell Labs and who described herself as “thinking in Unix”, Cherry contributed to Unix tools such as eqn, which assists in formatting mathematical texts.
Organizations like “Women Who Code” and “Girls Who Code” actively support women in learning shell scripting and automation.
Find the GitHub repo here.
If you find this blog helpful too, give back and show your support by clicking heart or like, share this article as a conversation starter and join my newsletter so that we can continue learning together and you won’t miss any future posts.
Thanks for reading until the end! If you have any questions or feedback, feel free to leave a comment.