The for
loop in Bash scripting is a powerful tool used to repeat commands and perform repetitive tasks efficiently. Whether you’re managing files, executing commands multiple times, or processing data, mastering the for
loop can significantly simplify your scripts.
In this article, we’ll walk through several examples of for
loops, starting with the basics and advancing to more complex applications.
Each example includes easy-to-understand explanations, tables, and links to help you understand Bash for
loops thoroughly.
In Bash, a for
loop allows us to iterate over a series of items and perform a command for each one. Syntax for the basic for
loop in Bash is:
for variable in list
do
command1
command2
...
done
The loop will execute each command block for each item in the list, using the variable
to represent each item in the sequence.
Let’s start with a simple example that prints numbers from 1 to 5.
for i in 1 2 3 4 5
do
echo "Number: $i"
done
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
In this example, the loop iterates through the numbers 1 to 5, printing each one with the echo
command.
We can use {}
to specify ranges, which is often simpler and more readable.
for i in {1..5}
do
echo "Count: $i"
done
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
This method is especially useful for long sequences since Bash will automatically fill in all numbers in the specified range.
Sometimes you may need to skip numbers in a loop, which is done by adding a step value {start..end..step}
.
for i in {0..20..5}
do
echo "Step Value: $i"
done
Step Value: 0
Step Value: 5
Step Value: 10
Step Value: 15
Step Value: 20
The for
loop increments by 5 instead of 1, making it efficient for specific tasks.
Bash loops are also ideal for processing arrays. Here’s an example with an array of fruits.
fruits=("apple" "banana" "cherry")
for fruit in "${fruits[@]}"
do
echo "Fruit: $fruit"
done
Fruit: apple
Fruit: banana
Fruit: cherry
This loop uses "${fruits[@]}"
to reference all elements in the array, iterating over each fruit name.
You can use for
loops to iterate through files in a directory. This is useful for file manipulation and batch processing.
for file in /path/to/directory/*
do
echo "Processing $file"
done
Processing file1.txt
Processing file2.txt
...
Replace /path/to/directory
with the directory path containing the files. The loop will display each file name in that directory.
Bash also supports a C-style for
loop structure, which can be handy for complex iterations.
for ((i=1; i<=5; i++))
do
echo "Iteration $i"
done
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
In this style, you can control the initialization, condition, and increment, making it more flexible for customized loops.
You can add conditional statements like if
statements inside a for
loop for greater control.
for i in {1..10}
do
if (( $i % 2 == 0 ))
then
echo "$i is even"
else
echo "$i is odd"
fi
done
1 is odd
2 is even
3 is odd
4 is even
...
In this example, the loop uses the if
condition to determine whether each number is odd or even.
Nested loops allow for more complex operations, like processing two lists simultaneously.
for i in {1..3}
do
for j in {a..c}
do
echo "Pair: $i$j"
done
done
Pair: 1a
Pair: 1b
Pair: 1c
Pair: 2a
Pair: 2b
Pair: 2c
Pair: 3a
Pair: 3b
Pair: 3c
Here, we loop over both numbers and letters to generate all combinations.
Count the number of lines in each text file in a directory.
for file in *.txt
do
lines=$(wc -l < "$file")
echo "$file has $lines lines"
done
This loop uses wc -l
to count the lines in each .txt
file in the current directory.
Rename all .jpg
files in a directory by appending _backup
to their names.
for file in *.jpg
do
mv "$file" "${file%.jpg}_backup.jpg"
done
This loop processes each .jpg
file, appending _backup
to the filename.
Loop Type | Syntax | Description |
---|---|---|
Basic For Loop | for var in list; do commands; done | Executes commands for each item in the list |
Range-Based Loop | for var in {start..end}; do commands; done | Loops over a numerical range |
C-Style For Loop | for ((init; cond; increment)); do commands; done | C-style looping for more complex conditions |
Array Loop | for var in "${array[@]}"; do commands; done | Loops over elements in an array |
Directory Loop | for file in /path/*; do commands; done | Iterates through all files in a directory |
Nested For Loop | for i in list1; do for j in list2; do commands; done; done | Executes loops within loops for complex tasks |
{1..N}
syntax is usually faster and easier to read.find
can improve performance.Bash for
loops are incredibly versatile and can be applied to a range of tasks, from processing lists and arrays to handling complex conditional logic. By understanding the syntax and applying these practical examples, you can make your scripts more efficient and adaptable for any command-line automation tasks.
लाल चंदन, जिसे रेड सैंडलवुड के नाम से भी जाना जाता है, एक अत्यधिक मूल्यवान…
2 एकड़ जमीन पर जैविक खेती से ₹75,000 तक कमाना सही योजना और मेहनत से संभव है इसके लिए ऐसी फसलें उगाएं जिनकी बाजार में ज्यादा मांग हो, जैसे टमाटर, पत्तेदार सब्जियां, पपीता, तुलसी, या एलोवेरा।आप विदेशी फसलें जैसे जुकिनी और केल भी उगा सकते हैं। खेत में रासायनिक खाद की जगह जैविक खाद जैसे गोबर खाद या वर्मीकम्पोस्ट का इस्तेमाल करें। फसल बदल-बदल कर उगाएं और दो फसलें साथ लगाएं ताकि मिट्टी की सेहत बनी रहे। कीटों को भगाने के लिए नीम का तेल या लहसुन का छिड़काव करें। जमीन का पूरा इस्तेमाल करें आप बेल वाली सब्जियां जैसे खीरा वर्टिकल तरीके से उगा सकते हैं। छोटी अवधि वाली फसलें, जैसे माइक्रोग्रीन्स, जल्दी पैसा कमा सकती हैं। पानी बचाने के लिए ड्रिप सिंचाई लगाएं और बारिश का पानी इकट्ठा करें। फसल को सीधे ग्राहकों को बेचें।किसान बाजार, ऑनलाइन प्लेटफॉर्म, और सब्सक्रिप्शन बॉक्स से अधिक मुनाफा कमा सकते हैं। अचार, जूस, या सूखे मसाले जैसे उत्पाद बनाकर फसल का मूल्य बढ़ाएं। सरकार की मदद जैसे सब्सिडी और योजनाओं का फायदा उठाएं। आप एग्री-टूरिज्म से भी अतिरिक्त कमाई कर सकते हैं। बाजार की मांग समझें, नई फसलें उगाने की कोशिश करें, और अन्य किसानों से जुड़े रहें। इस तरह, जैविक खेती छोटे क्षेत्र पर भी एक अच्छा कारोबार बन सकती है। 2 एकड़ जमीन पर जैविक…
The app "XVideoStudio Video Editor APK" can be risky. Many versions found online might have…
The "DNS_PROBE_FINISHED_NXDOMAIN" error indicates that your browser can't resolve the domain name of a website…
The HTTP 403 Error (Forbidden) means you don’t have permission to access the webpage or…
Many people think Bitcoin, a special kind of money that exists only online, could become…