Friday, May 17, 2024
10
rated 0 times [  10] [ 0]  / answers: 1 / hits: 3609  / 2 Years ago, sun, may 15, 2022, 4:48:02

I'm trying to run through two sequences in the same loop in my shell like below:



#!/bin/bash
for i in (1..15) and (20..25) ;
do
echo $i
......
.....other process
done


any idea how I can achieve this?


More From » command-line

 Answers
3

You only need brace expansion for that



$ for n in {1..3} {200..203}; do echo $n; done
1
2
3
200
201
202
203


We can pass a list to for (for i in x y z; do stuff "$i"; done).



So here, braces { } get the shell to expand your sequences into a list. You only need put a space between them, since the shell splits lists of arguments on those.


[#9965] Tuesday, May 17, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
oileweaty

Total Points: 337
Total Questions: 108
Total Answers: 105

Location: Western Sahara
Member since Mon, May 3, 2021
3 Years ago
;