how to use bash For loop in Linux

howtouselinux
Mar 19, 2021

--

For Loop over a number range in Bash

We can use the sequence expression to specify a range of numbers or characters by defining a start and the end point of the range.

  1. seq start end
  2. {start..end}

All the following for loop example iterates through all numbers from 0 to 3.

for i in {0..3}
do
echo "Number: $i"
done
for i in $(seq 0 3)
do
echo "Number: $i"
done
for i in `seq 0 3`
do
echo "Number: $i"
done

This is the output.

Number: 0
Number: 1
Number: 2
Number: 3

Check this post to learn more about how to use for loop in bash.

--

--

howtouselinux
howtouselinux

Written by howtouselinux

subscribe, please. We bring real-world experience, the latest trends, and DevOps tips here. contact me: https://forms.gle/dfhQfmTMFhtLAoaa9

No responses yet