If Statements
If statements execute code base on a condition. If the condition is true, the code block will run.
The condition is enclosed in square brackets [ ] and the statement ends with fi, which is if spelled backward, marking the end of the if block.
Example: Basic If Statement.
num=15
if [ $num -gt 10 ]; then
echo "Number is greater than 10"
fi
If...Else Statements
If...else statements provide a way to execute one block of code if a condition is true and another block if it is false.
The else keyword introduces the alternative block, and the statement ends with fi.
Example: If...Else Statement
num=8
if [ $num -gt 10 ]; then
echo "Number is greater than 10"
else
echo "Number is 10 or less"
fi
Nested if Statements
Nested if statements allow you to place an if statement inside another if statement, enabling more complex logic.
Each if block must be closed with its own fi.
Example: Nested If Statement.
num=5
if [ $num -gt 0 ]; then
if [ $num -lt 10 ]; then
echo "Number is between 1 and 9"
fi
fi
Loops
For Loops
For loops allow you to iterate over a list of items or a range of numbers. They are useful for repeating tasks a specific number of times.
The for keyword is followed by a variable name, a range of values, and a do keyword, which marks the start of the loop block.
Example: For Loop.
for i in {1..5}; do
echo "Iteration $i"
done
The loop runs 5 times, with i going from 1 to 5.
So the output will be:
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
While Loops
While loops execute a block of code as long as a specified condition is true.
They are useful for tasks that need to repeat until a certain condition changes.
The condition is enclosed in square brackets [ ], and the loop ends with done.
Example: While Loop.
count=1
while [ $count -le 5 ]; do
echo "Count is $count"
((count++))
done
This while loop also runs 5 times, incrementing count each time.
The output will be:
Count is 1
Count is 2
Count is 3
Count is 4
Count is 5
Once count becomes 6, the condition [ $count -le 5 ] is false, so the loop stops.
Until Loops
Until loops are similar to while loops, but they execute until a specified condition becomes true.
The condition is enclosed in square brackets [ ], and the loop ends with done.
Example: Until Loop.
count=1
until [ $count -gt 5 ]; do
echo "Count is $count"
((count++))
done
Here, it keeps looping until count is greater than 5.
So the output is:
Count is 1
Count is 2
Count is 3
Count is 4
Count is 5
When count reaches 6, the condition [ $count -gt 5 ] becomes true, and the loop stops.
Break and Continue
Break and continue statements are used to control loop execution. break exits the loop, while continue skips to the next iteration.
These statements are typically used inside conditional blocks to alter the flow of the loop.
Example: Break and Continue.
for i in {1..5}; do
if [ $i -eq 3 ]; then
continue
fi
echo "Number $i"
if [ $i -eq 4 ]; then
break
fi
done
This is the explanation of the above code.
When i = 1 - prints.
When i = 2 - prints.
When i = 3 - continue skips the echo, so nothing prints.
When i = 4 - prints, then break stops the loop.
i = 5 is never reached.
So the output will be:
Number 1
Number 2
Number 4
continue skips the current iteration, and break exits the loop completely.
Nested Loops
Nested loops allow you to place one loop inside another, enabling more complex iteration patterns.
Each loop must be closed with its own done.
Example: Nested Loops.
for i in {1..3}; do
for j in {1..2}; do
echo "Outer loop $i, Inner loop $j"
done
done
Let’s break it down:
i = 1 → j = 1, 2
i = 2 → j = 1, 2
i = 3 → j = 1, 2
The output will be:
Outer loop 1, Inner loop 1
Outer loop 1, Inner loop 2
Outer loop 2, Inner loop 1
Outer loop 2, Inner loop 2
Outer loop 3, Inner loop 1
Outer loop 3, Inner loop 2
Each outer loop iteration triggers the inner loop from start to finish.
Tomorrow, I’m going to review and practice the Bash scripting lessons I’ve already learned such as variables, data types, operators, if–else statements, and loops, so I can use Bash confidently. I want to strengthen what I’ve learned and get more comfortable writing scripts on my own.
Top comments (0)