codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐧

Linux & Bash

25 / 60 topics
23Scripting Basics24Variables25Control Structures26Functions27Input/Output Redirection28Debugging Scripts
Tutorials/Linux & Bash/Control Structures
🐧Linux & Bash

Control Structures

Updated 2026-05-15
10 min read

Control Structures

In this section, we will explore the essential control structures available in Bash scripting. These include conditional statements and loops, which are fundamental for controlling the flow of execution in scripts.

Introduction

Control structures allow you to make decisions based on certain conditions or repeat a set of commands multiple times. Understanding these concepts is crucial for writing effective and efficient Bash scripts.

Concept

Conditional Statements

Conditional statements enable your script to execute different blocks of code depending on whether a specified condition is true or false. The most common conditional statement in Bash is the if statement.

Basic Syntax

The basic syntax of an if statement is as follows:

if [ condition ]; then
    # commands if condition is true
fi
  • [ condition ]: This is a test command that evaluates to either true or false.
  • then: Marks the beginning of the block of code to execute if the condition is true.
  • fi: Marks the end of the if statement.

Example

Here's an example where we check if a number is greater than 10:

#!/bin/bash

number=15

if [ $number -gt 10 ]; then
    echo "Number is greater than 10"
fi
Terminal
Output

While Loop

The while loop continues to execute as long as a specified condition is true.

Basic Syntax
while [ condition ]; do
    # commands to be executed while the condition is true
done
Example

Here's an example where we count down from 5 to 1:

#!/bin/bash

count=5

while [ $count -gt 0 ]; do
    echo $count
    count=$((count - 1))
done
Terminal
Output
1
2
3
4
5

What's Next?

In the next section, we will explore functions in Bash scripting. Functions allow you to encapsulate a set of commands and reuse them throughout your script.


By mastering control structures, you can write more complex and dynamic scripts that respond to different conditions and perform repetitive tasks efficiently.


PreviousVariablesNext Functions

Recommended Gear

VariablesFunctions