# Control Structures in Shell Scripting

Control structures are essential components of shell scripting that allow you to control the flow of execution based on certain conditions or to repeat tasks. This blog post will explore conditional statements, loops, and case statements, providing you with the foundational knowledge needed to create more dynamic and powerful scripts.

## 1\. Conditional Statements

Conditional statements enable your scripts to make decisions based on specified conditions. The primary conditional statements in shell scripting are `if`, `else`, and `elif`.

### Basic `if` Statement

The simplest form of an `if` statement checks if a condition is true.

```plaintext
if [ condition ]; then
    # Commands to execute if condition is true
fi
```

### Example:

```plaintext
number=5

if [ $number -gt 0 ]; then
    echo "$number is positive."
fi
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728823539629/6415fd28-9a9c-4ec4-989f-b2c96f38106b.png align="center")

**Output:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728823589012/28ce3752-d9f7-4ea0-9f92-183a5613616c.png align="center")

### `if-else` Statement

The `else` clause allows you to define an alternative action if the condition is false.

```plaintext
if [ condition ]; then
    # Commands if true
else
    # Commands if false
fi
```

### Example:

```plaintext
number=-3

if [ $number -gt 0 ]; then
    echo "$number is positive."
else
    echo "$number is not positive."
fi
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728823629157/61315757-dff6-4f7a-aca5-f37fa2b76eb2.png align="center")

**Output:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728823673922/47faa713-12bb-4a2d-bd8e-6d1ad632ec98.png align="center")

### `elif` Statement

The `elif` (else if) statement lets you check multiple conditions sequentially.

```plaintext
if [ condition1 ]; then
    # Commands if condition1 is true
elif [ condition2 ]; then
    # Commands if condition2 is true
else
    # Commands if all conditions are false
fi
```

### Example:

```plaintext
number=0

if [ $number -gt 0 ]; then
    echo "$number is positive."
elif [ $number -lt 0 ]; then
    echo "$number is negative."
else
    echo "$number is zero."
fi
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728823710257/d1b02d37-be56-4edc-8d68-497e945484c7.png align="center")

**Output:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728823748773/4370f5f1-5209-4d0b-818e-dfcec651689b.png align="center")

### Condition Operators

Common condition operators include:

* `-eq` : Equal to
    
* `-ne` : Not equal to
    
* `-lt` : Less than
    
* `-gt` : Greater than
    
* `-le` : Less than or equal to
    
* `-ge` : Greater than or equal to
    

## 2\. Case Statements

The `case` statement provides a way to execute different commands based on the value of a variable, allowing for multi-conditional branching.

### Syntax:

```plaintext
case variable in
    pattern1)
        # Commands for pattern1
        ;;
    pattern2)
        # Commands for pattern2
        ;;
    *)
        # Default commands if no patterns match
        ;;
esac
```

### Example:

```plaintext
fruit="apple"

case $fruit in
    apple)
        echo "It's an apple."
        ;;
    banana)
        echo "It's a banana."
        ;;
    *)
        echo "Unknown fruit."
        ;;
esac
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728823811801/a84394b1-edef-4b56-a135-c0d487216b37.png align="center")

**Output:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728823876956/3ab905e0-43e4-4f70-8521-fd192071dc63.png align="center")

# 3\. Loops

Loops allow you to execute a block of code multiple times, making it easier to handle repetitive tasks. The most common types of loops in shell scripting are for, while, and until.

## `for` Loop

The `for` loop is used to iterate over a list of items. It is particularly useful when you know the number of iterations beforehand.

### Syntax:

```plaintext
for variable in list; do
    # Commands to execute for each item
done
```

### Example:

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

In this example, the loop iterates over the sequence `{1..5}`, and on each iteration, it prints the current value of `i`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728823927905/24bd687f-66ca-4b5c-b1b6-1eed7f526e49.png align="center")

**Output:**

```plaintext
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728823965539/fb3212c9-0f1f-49b8-8aca-599ab85a43a4.png align="center")

## `while` Loop

The `while` loop continues executing as long as a specified condition is true. It’s ideal when the number of iterations is not predetermined, and you want to repeat a task until a certain condition changes.

### Syntax:

```plaintext
while [ condition ]; do
    # Commands to execute while condition is true
done
```

### Example:

```plaintext
count=1

while [ $count -le 5 ]; do
    echo "Count: $count"
    count=$((count + 1))  # Increment the count
done
```

In this example, the loop prints the value of `count` and increments it by 1 on each iteration. The loop stops when `count` becomes greater than 5.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728824013552/2bbfbd02-2438-4ae6-bcf4-7bd8b3ee0eda.png align="center")

**Output:**

```plaintext
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728824059517/63df461f-c486-4b99-ade5-b76f632e537b.png align="center")

## `until` Loop

The `until` loop is similar to the `while` loop, but it continues executing until a specified condition becomes true. This means that it keeps looping while the condition is false and stops when the condition is true.

### Syntax:

```plaintext
until [ condition ]; do
    # Commands to execute until condition is true
done
```

### Example:

```plaintext
count=1

until [ $count -gt 5 ]; do
    echo "Count: $count"
    count=$((count + 1))  # Increment the count
done
```

In this example, the loop prints the value of `count` and increments it by 1 on each iteration. The loop stops when `count` becomes greater than 5.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728824107612/e437f325-8464-478c-a045-aaa1bb33a7aa.png align="center")

**Output:**

```plaintext
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728824143177/69f78cc6-9fbb-4543-8636-e28ddee515b4.png align="center")

## Conclusion

Understanding control structures is vital for effective shell scripting. By using conditional statements, loops, and case statements, you can create scripts that are dynamic and adaptable to various scenarios.

Practice implementing these control structures in your scripts to enhance your scripting skills and automate tasks efficiently!
