Jon
Jon

Reputation: 1

Range with skipped value

Can I do this with one bash for statement? I have a range {1..8} but I want to take out 4. I know I can do this with two for statements:

for i in {1..3} blah blah blah
for i in {5..8} blah blah blah

I know I can add an if statement in the for loop. But was wondering if I could possibly use a short regex?

Upvotes: 0

Views: 35

Answers (2)

tshiono
tshiono

Reputation: 22022

I'm not sure if this is what you want but we can simply say:

for i in {1..3} {5..8}; do
    echo $i
    # or do whatever you like ..
done

Hope this helps.

Upvotes: 1

strikingLoo
strikingLoo

Reputation: 1

Well from the question I can't really make out whether you wish to divide them in half (1...4 and 5...8) or split them into odds and evens (1,3,5,7 on one side and the rest in the other).

In the first case you could do a loop through the whole thing and ask if( i < 5 ) whereas in the second one, you could check if( i % 2 == 0 ). Hope that helped!

Upvotes: 0

Related Questions