• Skip to main content
  • Skip to primary sidebar
  • Home
  • WordPress
  • web Hosting
  • linux
  • mysql
  • nginx
  • apache2
  • devops

Raju Ginni

wordpress tutorials seo hosting etc

You are here: Home / Linux sysadmin tutorials linux system administrator / Control Structures in Shell Scripting

Control Structures in Shell Scripting

Control structures allow a script to make decisions and execute code conditionally or repeatedly. There are three main types:

  1. Conditional Statements (if, case)

  2. Loops (for, while, until)

  3. Branching (break, continue)


Table of Contents

Toggle
  • 1. Conditional Statements
    • a) If-Else Statement
      • Syntax:
      • Example:
    • b) Case Statement
      • Syntax:
      • Example:
  • 2. Loops
    • a) For Loop
      • Syntax:
      • Example:
      • Using a Range:
      • Using C-style Syntax:
    • b) While Loop
      • Syntax:
      • Example:
    • c) Until Loop
      • Syntax:
      • Example:
  • 3. Loop Control Statements
    • a) Break Statement
    • b) Continue Statement
    • 🚀 Putting It All Together: A Complete Example
    • 🔥 Key Takeaways

1. Conditional Statements

a) If-Else Statement

Used for decision-making based on conditions.

Syntax:

bash
if [ condition ]; then
# Code to execute if condition is true
elif [ another_condition ]; then
# Code for another condition
else
# Code if no condition matches
fi

Example:

bash
#!/bin/bash
echo -n "Enter a number: "
read num

if [ $num -gt 10 ]; then
echo "Number is greater than 10"
elif [ $num -eq 10 ]; then
echo "Number is exactly 10"
else
echo "Number is less than 10"
fi

📌 Common Comparison Operators:

Operator Description
-eq Equal to
-ne Not equal to
-gt Greater than
-lt Less than
-ge Greater than or equal to
-le Less than or equal to

b) Case Statement

Used when multiple conditions need to be checked, similar to switch in other languages.

Syntax:

bash
case $variable in
pattern1)
# Code to execute
;;
pattern2)
# Code to execute
;;
*)
# Default case
;;
esac

Example:

bash
#!/bin/bash
echo "Enter a fruit name: "
read fruit

case $fruit in
apple)
echo "You chose Apple";;
banana)
echo "You chose Banana";;
orange)
echo "You chose Orange";;
*)
echo "Unknown fruit";;
esac


2. Loops

Loops are used to execute a block of code multiple times.

a) For Loop

Repeats execution a set number of times.

Syntax:

bash
for variable in list; do
# Code to execute
done

Example:

bash
#!/bin/bash
for i in 1 2 3 4 5; do
echo "Iteration: $i"
done

Using a Range:

bash
for i in {1..5}; do
echo "Number: $i"
done

Using C-style Syntax:

bash
for ((i=1; i<=5; i++)); do
echo "Loop iteration: $i"
done

b) While Loop

Executes while a condition is true.

Syntax:

bash
while [ condition ]; do
# Code to execute
done

Example:

bash
#!/bin/bash
count=1
while [ $count -le 5 ]; do
echo "Count: $count"
((count++))
done

c) Until Loop

Similar to while, but runs until the condition becomes true.

Syntax:

bash
until [ condition ]; do
# Code to execute
done

Example:

bash
#!/bin/bash
num=1
until [ $num -gt 5 ]; do
echo "Number: $num"
((num++))
done

3. Loop Control Statements

a) Break Statement

Exits a loop immediately.

bash
#!/bin/bash
for i in {1..10}; do
if [ $i -eq 5 ]; then
echo "Breaking at $i"
break
fi
echo "Iteration: $i"
done

b) Continue Statement

Skips the rest of the loop’s body and moves to the next iteration.

bash
#!/bin/bash
for i in {1..5}; do
if [ $i -eq 3 ]; then
echo "Skipping $i"
continue
fi
echo "Processing $i"
done

🚀 Putting It All Together: A Complete Example

bash
#!/bin/bash

echo "Enter a number (1-5):"
read num

if [ $num -ge 1 ] && [ $num -le 5 ]; then
echo "Valid number, looping..."
for i in $(seq 1 $num); do
if [ $i -eq 3 ]; then
echo "Skipping 3"
continue
fi
echo "Iteration: $i"
done
else
echo "Invalid input!"
fi


🔥 Key Takeaways

✅ If-Else: Used for decision-making.
✅ Case Statement: Simplifies multiple conditions.
✅ Loops: for, while, and until automate repetitive tasks.
✅ Break & Continue: Control loop execution flow.

1️⃣ If-Else Statements (More examples, nested conditions)
2️⃣ Case Statements (Pattern matching, advanced use cases)
3️⃣ For Loop (Iterating over arrays, files, numbers)
4️⃣ While/Until Loops (Real-world automation examples)
5️⃣ Break & Continue (Use in nested loops)
6️⃣ A Complete Script Combining All Concepts

Primary Sidebar

Linux sysadmin tutorials linux system administrator

  • top 10 apt & apt-get commands (most used) apt vs apt-get
  • If-Else Statements in Shell Scripting
  • linux commands pdf (files & Directories, zip & unzip process, search etc)
  • Find Files with Specific Text on Linux grep find command
  • linux performance tuning inode limit file descriptors tco, kernel etc
  • Variables and Data Types in Shell Scripting
  • Top 10 most used Cat commands with examples (create, view, append files)
  • Ip tables / ufw / firewall d commands for block port ip rate limiting
  • Top 10 zip / tar commands to compress & extract files in linux
  • TOP 10 mv & cp commands in linux to move & copy files in Linux
  • Top 10 GREP Commands in linux to search files directory words strings
  • lsof netstat commands to know listening ports in linux 3 ways
  • Upgrade Ubuntu from 18.04 (19.10) to 20.04 LTS command line or gui server | desktop
  • 3 Ways (SCP, rsync, Sftp) linux server migration between two remote server apache nginx
  • linux system specs commands (CPU, Memory, Disk )speed, type. manufacture
  • linux sysctl command tweaks & hardening
  • linux security limits.conf deciding user limits process limits for nginx server
  • ulimit linux unlimited command unlimto set & know user limits open files file descriptor max user process etc.
  • red hat linux certification cost jobs salary syllabus courses fees
  • ufw firewall commads allow port enable disable ubuntu 20.04
  • ddos attack prevention
  • change ssh port in linux - avoid sshd ddos attacks
  • ping command
  • memcached install ubuntu wordpress
  • check linux version (lsb_release -a) ubuntu debian 32 or 64 bit
  • rsync command linux with examples comparison to scp
  • how to uninstall package in linux ubuntu rpm, yum apt-get
  • increase open file limit linux File descriptor ft nginx , mysql, lemp
  • remove repository ubuntu
  • htop commad memory details virtual vs shard vs resident
  • chown command in Linux with Examples
  • Kill PHP process
  • VIrtual Memory vs RSS Memory vs Shared memory in Linux
  • oom killer fixing it by configuration linux ubuntu
  • Install Lemp nginx mysql php fpm Stack on Debian 11 with repository
  • connect two remote servers linux command line
  • auto start after oom killer Mysql & php fpm nginx etc ubuntu wth systemd or cron job
  • load average Linux 1, 5, 15 min 2,4,8 cores explained
  • Control Structures in Shell Scripting
  • Shell Scripting Roadmap for Beginners to Advanced
  • awk commands with practical examples
  • Shell Scripting Tutorial for Beginners 🚀
  • find Command in Linux with Examples
  • sed Command in Linux with Examples (Beginner to Advanced)
  • Linux Text processing commands in with Examples
  • linux disk management commands
  • fdisk command in linux with examples
  • how to add a new disk in linux
  • Linux mount Command with Examples
  • fstab options with examples
  • Top 50 Shell Scripting Interview Questions and Answers
  • Linux Networking Interview Questions and Answers
  • Linux Networking Commands Cheat Sheet with Examples pdf
  • Netstat & SS Commands cheat sheet with examples Interview Questions
  • Nmap Cheat Sheet – Network Scanning & Security
  • Bash Brackets ([], (), {}, $( ), $(( ))) – Types, Uses & Examples

hi i am raju ginni, primalry i manage wordpress websites on GCP cloud platform as a cloud engineer, and create content on passionate things.
you can follow me on youtbe

© 2025 - All Rights Reserved Disclaimer & Privacy Policy