Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash loop: for loop in older bash versions

Tags:

bash

I have an older version of bash: 3.2.25 I am trying to write a for loop:

#!/bin/bash
filename="K"

for k in {2..20..2}
do
  filename=$filename$k
done

This should give me:

K2
K4
.
.
K20

But this is what it gives me:

K{2..20..2}
K{2..20..2}
.
.
K{2..20..2}

Is it because it is an older version of bash? How can I get it to work? Listing all the values is not a solution because I'm going to have to use {5..201}.

like image 468
sodiumnitrate Avatar asked Sep 22 '26 11:09

sodiumnitrate


1 Answers

That was added in bash 4, I believe.

You can do:

for (( k=5; k<=201; k+=2 )); do
    filename=$filename$k
done
like image 165
Gray Avatar answered Sep 24 '26 06:09

Gray



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!