Bash for loop

Example

The simplest example of a for loop

#!/bin/bash for i in $(seq 1 10); do echo $i done

./for_loop.sh

1 2 3 4 5 6 7 8 9 10

for … in loop over an array

To create an array you need to use declare -a.

The following syntax is used to iterate over the elements

for i in "${arr[@]}"

Note the absence of commas between the elements.

#!/bin/bash declare -a urls=("https://beget.com" "https://heihei.ru" "https://testsetup.site" ) for url in "${urls[@]}" do echo "$url" done

./for_array.sh

https://beget.com https://heihei.ru https://testsetup.site

Article author: Andrei Olegovich