How to produce a range with step n in bash? (generate a sequence of numbers with increments)

SilentGhost picture SilentGhost · Jun 8, 2009 · Viewed 121.8k times · Source

The way to iterate over a range in bash is

for i in {0..10}; do echo $i; done

What would be the syntax for iterating over the sequence with a step? Say, I would like to get only even number in the above example.

Answer

chaos picture chaos · Jun 8, 2009

I'd do

for i in `seq 0 2 10`; do echo $i; done

(though of course seq 0 2 10 will produce the same output on its own).

Note that seq allows floating-point numbers (e.g., seq .5 .25 3.5) but bash's brace expansion only allows integers.