НаукаТехнологии

used bash commmand lang for count iteration

Here are several clean and commonly used ways to write iteration counters (counting loops) in Bash scripts:

1. Most popular & cleanest (recommended)

Bash
#!/usr/bin/env bash

count=10

for ((i=1; i<=count; i++)); do
    echo "Iteration $i / $count"
done

2. While loop version (very explicit)

Bash
#!/usr/bin/env bash

i=1
total=15

while [ $i -le $total ]; do
    echo "Step $i of $total"
    ((i++))
    # or: i=$((i+1))
done

3. C-style for loop (very common in 2024/2025 scripts)

Bash
for ((i = 0; i < 8; i++)); do
    echo "index = $i"
done

4. Counting down (reverse)

Bash
for ((i=10; i>=1; i--)); do
    echo "T minus $i..."
    sleep 1
done
echo "Go!"

5. With step size (not just +1)

Bash
# count from 0 to 100 step 10
for ((i=0; i<=100; i+=10)); do
    echo "$i%"
done

6. Quick one-liner version (often used in scripts)

Bash
for i in {1..5}; do echo "Test $i"; done

# or with seq (more flexible)
for i in $(seq 1 2 20); do echo $i; done    # 1 3 5 7 ... 19

Quick reference table – choose style according to your taste

StyleSyntaxBest forZero-based?Most readable?
C-style forfor ((i=1; i<=n; i++))Most scripts 2020–2026Optional★★★★★
seq + forfor i in $(seq 1 10)Quick prototypingNo★★★☆☆
brace expansionfor i in {1..10}Small fixed rangesNo★★★★☆
while loopwhile ((i<=n))When logic is more complexOptional★★★☆☆

😄 do you have some specific condition (like «only even numbers», «until some command succeeds», etc.)

What's your reaction?

Excited
0
Happy
0
In Love
0
Not Sure
0
Silly
0

Вам понравится

Смотрят также:Наука

Оставить комментарий