1

For example:

for s in 1..5,20..23,97..99;
do
   echo $s;
done
1..5,
20..23,
97..99

How to make it loop through 1,2,3,4,5,20,21,22,23,97,98,99?

2 Answers 2

4

You need to use brace expansion { } syntax:

for num in {1..5} {20..23} {97..99}; do
   echo "$num"
done
Sign up to request clarification or add additional context in comments.

2 Comments

might be better in full form {{1..5},{20..23},{97..99}} preventing accidents when space is missing {1..5} {20..23}{97..99} which will give you 1 2 3 4 5 2097 2098 2099 2197 2198 2199 2297 2298 2299 2397 2398 2399
@karakfa Well, either you have to remember to use a space or to use a comma. {{1..5},{20..23}{97..99}} would do the same.
2

You can use seq,

 for s in $(seq 1 5) $(seq 20 23) $(seq 97 99);
 do
   echo $s;
 done

Result:

1
2
3
4
5
20
21
22
23
97
98
99

brace expansion { } is another choice.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.