Nested Loop In ShellScript
Nested for loops means loop within loop. They are useful for when you want to repeat something several times for several things.
example:
#!/bin/bash
initNumber=1
while [[ ${initNumber} -lt 3 ]]
do
for i in item1 item2 item3
do
echo "${initNumber} - ${i}"
done
((initNumber++))
done
output:
┌──(gaurav㉿learning-ocean)-[~/shellscript-youtube]
└─$ ./nested-loop.sh
1 - item1
1 - item2
1 - item3
2 - item1
2 - item2
2 - item3
nested loop with break level example
#!/bin/bash
initNumber=1
while [[ ${initNumber} -lt 3 ]]
do
for i in item1 item2 item3
do
echo "${initNumber} - ${i}"
if [[ $i == item2 ]]
then
break 2
fi
done
((initNumber++))
done
output:
┌──(gaurav㉿learning-ocean)-[~/shellscript-youtube]
└─$ ./nested-loop.sh
1 - item1
1 - item2