
Bash scripting becomes truly powerful when you can automate repetitive tasks. Loops are your best friend for this! Today, we'll explore three essential loop types in Bash: while, for, and select, with clear and simple examples.
1. The while Loop: Repeating Until a Condition is Met
The while loop executes a block of code repeatedly as long as a specified condition remains true.
while [ condition ]; do
# Code to execute
done
[ condition ]: The condition that determines whether the loop continues.
do: Marks the start of the loop's code block.
done: Marks the end of the loop.
Example 1: Counting to 5
#!/bin/bash
count=1
while [ $count -le 5 ]; do
echo "Count: $count"
count=$((count + 1))
done
Explanation:
We initialize count to 1.
The loop continues as long as count is less than or equal to 5.
Inside the loop, we print the current count and increment it by 1.
2. The for Loop: Iterating Over a List
The for loop iterates over a list of items, executing the loop's code block for each item.
for variable in list; do
# Code to execute
done
variable: A variable that takes on the value of each item in the list.
list: The list of items to iterate over.
Example 2: Printing a List of Fruits
#!/bin/bash
fruits="apple banana orange"
for fruit in $fruits; do
echo "Fruit: $fruit"
done
Explanation:
We define a string fruits containing a list of fruits.
The loop iterates over each fruit in the list, printing it.
Example 3: Iterating Through a Range
#!/bin/bash
for i in {1..3}; do
echo "Number: $i"
done
Explanation:
{1..3} creates a sequence of numbers from 1 to 3.
The loop iterates through each number in the sequence.
3. The select Loop: Creating Menus
The select loop is used to create interactive menus for user input.
select variable in list; do
# Code to execute
break
done
variable: A variable that takes on the value of the selected item.
list: The list of menu options.
break: Exits the loop after a selection.
Example 4: Simple Menu
#!/bin/bash
select choice in "Option 1" "Option 2" "Quit"; do
case $choice in
"Option 1")
echo "You selected Option 1"
break
;;
"Option 2")
echo "You selected Option 2"
break
;;
"Quit")
echo "Exiting..."
break
;;
*)
echo "Invalid choice"
;;
esac
done
Explanation:
The select loop displays a menu with three options.
The user's selection is stored in the choice variable.
The case statement handles the selected option.
break exits the loop after a valid selection.
Example 5: Looping through files in a directory.
#!/bin/bash
for file in *.txt; do
echo "Processing file: $file"
#Add your code here to process the file.
done
Explanation:
*.txt will create a list of all files in the current directory ending in .txt.
The for loop will then iterate through each file in the list.
Key Takeaways:
while loops repeat until a condition is false.
for loops iterate over a list of items.
select loops create interactive menus.
By mastering these loop types, you'll be able to create more complex and efficient Bash scripts. Practice these examples and experiment with your own loops to solidify your understanding.
Comments