Bash
Loops
for and while loops.
By EZ4Code Team
bashloop
Code
#!/bin/bash
# for loop: list
for item in apple banana cherry; do
echo "Fruit: $item"
done
# for loop: range
for i in {1..5}; do
echo "Number: $i"
done
# for loop: step
for i in {0..10..2}; do
echo "Even: $i"
done
# for loop: C-style
for ((i=0; i<5; i++)); do
echo "Index: $i"
done
# for loop: iterate files
for file in *.txt; do
echo "Processing: $file"
done
# while loop
count=0
while [ $count -lt 5 ]; do
echo "Count: $count"
((count++))
done
# while read line
while IFS= read -r line; do
echo "Line: $line"
done < file.txt
# until loop
n=0
until [ $n -ge 5 ]; do
echo "n=$n"
((n++))
done
# break and continue
for i in {1..10}; do
[ $i -eq 5 ] && break
[ $((i % 2)) -eq 0 ] && continue
echo "Odd: $i"
done
# Infinite loop
while true; do
echo "Running..."
sleep 1
doneExplanation
Bash supports for/while/until loops, iterating over lists, ranges, files; break/continue control flow.
More Bash Snippets
Variables and Arrays in Bash
Assign variables, use command substitution, and work with arrays in Bash.
File Operations
File and directory management.
Text Processing
Text processing with grep, sed, awk.
Conditionals
if and case conditional statements.
Functions
Function definition and parameters.
Arrays
Bash array operations.