Shell Scripting Made EASY


🐚 What is Shell Scripting?
💬 Definition
Shell scripting is the practice of writing a series of commands for the shell (command-line interpreter) to execute automatically. These commands are written in a plain text file, which is referred to as a shell script.
📦 What is a Shell?
A shell is a program that provides a text-based interface for users to interact with the operating system.
Popular shells include:
Shell | Description |
sh | Bourne Shell (original UNIX shell) |
bash | Bourne Again SHell (most common, Linux default) |
zsh | Z Shell (more advanced features) |
csh | C Shell (uses C-like syntax) |
fish | Friendly Interactive Shell (user-friendly design) |
📜 What is a Shell Script?
A shell script is a file that contains a sequence of shell commands that are executed in order.
Example:
bash
Copy
#!/bin/bash
echo "Starting backup..."
tar -czf backup.tar.gz /home/user
echo "Backup complete."
🔍 What’s Happening Here?
● #!/bin/bash: Tells the system to use the Bash shell.
● echo: Prints messages to the screen.
● tar: Compresses the /home/user directory into a .tar.gz file.
🎯 Why Use Shell Scripting?
Shell scripting is used to:
● ✅ Automate repetitive tasks (backups, updates, deployments)
● ✅ Configure system settings
● ✅ Run multiple commands as a single program
● ✅ Schedule jobs with tools like cron
● ✅ Quickly test logic or prototypes
🛠️ Real-Life Examples
Task | How Shell Script Helps |
Daily backups | Automatically compress important files at night |
Server setup | Install packages, configure services, start apps |
Log analysis | Parse and summarize log files |
Deployment | Pull code, build Docker image, start containers |
CI/CD pipeline | Run tests, build apps, deploy to servers |
🧠 Key Characteristics of Shell Scripts
● Plain text files (.sh)
● Run using bash scriptname.sh or ./scriptname.sh
● Start with a shebang (#!/bin/bash) to specify the interpreter
● Can use:
○ **Variables
○ Conditions* (if, else)*
○ Loops (for, while)
○ **Functions
○ Command substitution* ($(command))*
🧪 Example: Simple Script
bash
Copy
#!/bin/bash
echo "Welcome to Shell Scripting"
date
uptime
This script:
- Greets the user
- Shows the current date
- Displays how long the system has been running
💡 Summary
Feature | Purpose |
Shell | Interface to run OS commands |
Script | File with commands for automation |
Bash | Most commonly used shell in Linux/DevOps |
Scripting | Saves time, reduces manual errors, boosts efficiency |
🧠 Introduction to Bash Syntax
(For Shell Scripting Beginners)
🧾 1. Variables in Bash
Variables are used to store data for reuse. In Bash, you don’t need to declare a type (everything is treated as a string unless used in arithmetic).
🔧 Syntax
bash
Copy
variable_name=value
⚠️ No spaces before or after the equal sign.
🧪 Example
bash
Copy
name="DevOps Student"
echo "Welcome, $name!"
🔍 Output
Copy
Welcome, DevOps Student!
📥 2. User Input
Use the read command to take input from the user.
🧪 Example
bash
Copy
#!/bin/bash
read -p "Enter your name: " username
echo "Hello, $username!"
🔍 What’s Happening?
● read -p: Prompts the user and stores input in a variable.
● $username: Prints what the user entered.
📤 3. Output (echo)
The echo command displays text or variables.
🧪 Example
bash
Copy
echo "Welcome to Bash scripting"
You can also format text:
bash
Copy
echo -e "Line 1\nLine 2"
❓ 4. Conditional Statements
Bash uses if, then, else, and fi to control decision-making.
🧪 Example
bash
Copy
#!/bin/bash
read -p "Enter username: " user
if [ "$user" == "admin" ]; then
echo "Access granted"
else
echo "Access denied"
fi
🔍 Breakdown:
● [ "$user" == "admin" ]: Checks if user is "admin"
● then: Executes block if true
● else: Executes if false
● fi: Ends the if statement
🔁 5. Loops
Loops help automate repetitive tasks.
🔁 For Loop
bash
Copy
for i in {1..5}
do
echo "Iteration $i"
done
🔁 While Loop
bash
Copy
count=1
while [ $count -le 3 ]
do
echo "Count is $count"
((count++))
done
🧩 6. Functions
Functions allow you to reuse blocks of code.
🧪 Example
bash
Copy
greet() {
echo "Hello, $1"
}
greet "DevOps Engineer"
● $1 is the first argument to the function.
⚙️ 7. Command Substitution
You can run a command and use its output in a variable.
🧪 Example
bash
Copy
current_date=$(date)
echo "Today is $current_date"
🛡️ 8. Exit Status ($?)
Every command returns a status code: 0 = success, non-zero = failure.
🧪 Example
bash
Copy
ls /some/folder
echo "Exit status was $?"
✅ Summary Table
Concept | Syntax | Example |
Variable | x=5 | echo $x |
Input | read var | read -p "Enter:" name |
Output | echo | echo "Hi" |
Condition | if ... fi | if [ $x -eq 5 ]; then |
Loop | for/while | for i in {1..5} |
Function | func() {} | greet John |
Command Sub. | $(command) | $(date) |
🧠 Your First Task
Write a script that:
- Asks for the user’s name
- Greets them
- Shows the current date and uptime
Example structure:
bash
Copy
#!/bin/bash
read -p "Enter your name: " name
echo "Hi, $name!"
echo "Today is: $(date)"
echo "System uptime:"
uptime
See the outcome result below
Subscribe to my newsletter
Read articles from Israel Adeyemo directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
