December 9, 2022

Simple for loop with Bash

Ever had the need to just run some command(s) a few times from the terminal? Here’s one simple oneliner and one multiline example of how to do it:

Oneliner:

for i in {1..5}; do echo "Hello, world"; done

## If you need to access the index variable:
for i in {1..5}; do echo "The index is $i"; done

Multiline:

for i in {1..5}
do
  multiplied=$((i * 2))
  echo "$i * 2 = $multiplied"
done