Monday, May 11, 2015

For loop with discontinuous numbers

To make a for loop in bash with e.g. 7 consecutive numbers from 1-7 do:

for j in {1..7}
do
echo $j
done


1
2
3
4
5
6
7



But if you want to make a loop that contains discontinuous numbers 1-7 and 14-15 simply add:

for j in {1..7} 14 15
do
echo $j
done


1
2
3
4
5
6
7

14
15

or:


for j in {1..7} {14..15}
do
echo $j
done


1
2
3
4
5
6
7

14
15

No comments:

Post a Comment