A bash counter is usually an integer you bump each time around a loop: a bash
loop counter in a for ((…)) header, a bash while loop counter you
increment in the body, or a bash script counter that tracks how many files,
lines, or matches you processed. This page ties those patterns together; for every
detail on ++ vs pre-increment and $(( )) vs (( )), read
bash increment variable.
I re-ran the snippets below on Ubuntu 25.04, kernel 6.14.0-37-generic, Bash 5.2.37.
Bash for loop counter (for (( )))
The clearest bash for loop counter is the C-style for (( i=0; i<n; i++ ))
form: the counter lives in the loop header.
for ((i = 0; i < 3; i++)); do
echo "i=$i"
donei=0
i=1
i=2You can also advance with ((i+=1)) or let i+=1 in the third slot or inside
the body if you need irregular steps.
Bash while loop counter
A bash while loop counter starts before the loop and moves inside the body:
n=0
while ((n < 3)); do
echo "n=$n"
((n++))
donen=0
n=1
n=2Use ((n+=1)) for steps other than one. Avoid n=$((n++)) in assignments:
$((var++)) returns the old value, so it is a poor way to “increment n and
pass the new value around” unless you really mean post-increment semantics.
Bash until loop counter
Same idea as while, but the guard is inverted:
n=0
until ((n > 2)); do
echo "n=$n"
((n += 1))
donen=0
n=1
n=2Bash for count over a list (not only numeric indexes)
bash for count often means “how many items did I touch?” when you loop over strings or paths. You can keep a separate counter:
c=0
for _ in a b c; do
echo "c=$c"
((c++))
donec=0
c=1
c=2Or use the index from {arr[@]} if you already have an array—see
bash for loop for more list patterns.
Bash script counter: example over files
Here the counter labels each file as you walk a glob:
dir=$(mktemp -d)
touch "$dir"/{a,b,c}.txt
i=0
for f in "$dir"/*.txt; do
echo "file $((++i)): $(basename "$f")"
done
rm -rf "$dir"file 1: a.txt
file 2: b.txt
file 3: c.txt$((++i)) bumps i before the value is used, which reads naturally as “first file
is number 1”. For “0-based index printed after increment”, use i++ on its own
line instead of inside echo.
let, (( )), and $(( )) (quick recap)
(( n++ ))or(( ++n ))— arithmetic command, no$needed.n=$((n + 1))— classic assignment with arithmetic expansion.let "n = n + 1"— older style; quotes help when tokens look like options.
All of these show up in shell script counter loops; pick one style per script so readers are not bounced between idioms.
Related loops and deeper reads
- bash for loop
- bash while loop
- bash until vs while loop
- bash increment variable for
+=,++placement, and pitfalls. - Stack Overflow discussion: Adding a counter in a shell script
Summary
A bash counter is an integer you move each iteration: use a bash for loop
counter in for ((i=0; i<n; i++)), a bash while loop counter (or until)
with ((i++)) in the body, or a bash for count variable when you loop over
values instead of indexes. For bash increment counter mechanics, prefer (( ))
and avoid confusing $((var++)) assignments unless you understand post-increment.
bash script counter patterns apply whenever you need human-readable sequence
numbers alongside real work (files, rows, checks).

