0

I was trying to print out some numbers from 1 to 20 with an increment of 2

#!/bin/bash

for i in {0..20..2} 
do
    echo $i
done

and this is what it has printed out

{0..20..2}

what I am doing wrong?

5
  • 4
    What version of bash are you using? Setting the step value is only after v4.0... Commented Oct 12, 2022 at 7:47
  • thank you very much. This was the reason. Commented Oct 12, 2022 at 7:59
  • 2
    You may consider using a C-style for loop: for ((i=0; i<=20; i+=2)); do echo $i; done Commented Oct 12, 2022 at 8:07
  • Or seq 0 2 20 Commented Oct 12, 2022 at 10:52
  • @Jon, ...suboptimal. seq isn't part of bash and isn't specified by POSIX, so you pay external-executable startup costs, and whether it works (and how it works) is entirely at the grace of your operating system vendor. Commented Oct 12, 2022 at 12:59

1 Answer 1

1

Using the built-in for loops syntax:

#!/bin/bash
for (( i=0; c<=20; c+=2 ))
do
    echo $i
done
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, using the built-in for loop probably is a better idea.

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.