208

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.

3
  • 1
    Even numbers, multiply i by 2. :P Jun 8, 2009 at 17:38
  • 1
    multiplication is rather ugly, I should say Jun 8, 2009 at 18:18
  • 2
    @Omnifarious I would not be so sure. OS X still comes with 3.2, for example. I would say the C-style loop should be accepted. Oct 5, 2018 at 17:21

6 Answers 6

261

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.

4
  • 5
    I'll hazard a guess that the downvote was due to your answer being generic to 'sh', and not specific to 'bash'. The pure Bash approach {begin end step} performs just a little better.The older 'seq' method's handy on older or smaller-memory systems like busybox. I did upvote both your and TheBonsai's answer. :) Feb 3, 2015 at 2:12
  • 11
    Prefer $(...) to backquotes. Backquotes do not nest sanely. Aug 23, 2018 at 17:07
  • seq 1000000 1000010 only does floats, actually - there's no way to make it do integers! Brace expansion works.
    – hmijail
    Jun 23, 2020 at 23:06
  • How could I save result(output) into a file?
    – eawedat
    May 18, 2021 at 15:23
156

Bash 4's brace expansion has a step feature:

for {0..10..2}; do
  ..
done

No matter if Bash 2/3 (C-style for loop, see answers above) or Bash 4, I would prefer anything over the 'seq' command.

7
  • 1
    and btw, do you know if bash4 is default on any major OS? Jun 8, 2009 at 18:03
  • 3
    Bash4 still isn't mainstream, no. Why not seq? Well, let's say it with the words of the bot in the IRC channel #bash: "seq(1) is a highly nonstandard external command used to count to 10 in silly Linux howtos."
    – TheBonsai
    Jun 8, 2009 at 18:11
  • 1
    my understanding is that seq is a part of coreutils. what is non-standard about it? arguments? thanks for your help. Jun 8, 2009 at 18:14
  • 5
    These arguments may or may not count for you: * there are enough systems without GNU coreutils (but Bash installed) * you create an unneeded external process * you rely on the idea that all 'seq' do what your 'seq' does * it's not standardized by the ISO
    – TheBonsai
    Jun 8, 2009 at 18:28
  • 4
    @becko If the step is stored in the variable i, then you can't do for {0..10..${i}} .. it fails. May 17, 2014 at 8:18
100

Pure Bash, without an extra process:

for (( COUNTER=0; COUNTER<=10; COUNTER+=2 )); do
    echo $COUNTER
done
5
  • 14
    +1 Because the step can be replaced by a variable too. May 17, 2014 at 8:17
  • 1
    I think this is really what most people likely want -- a simple way to do a loop with a prescribed step value. It sounds simpler than all of the seq based answers, and is a clearer syntax than Bash4 brace expansion, and looks like it would allow for variables (I haven't tried that, but the syntax definitely suggests that).
    – tobylaroni
    Mar 5, 2019 at 15:02
  • Works in Bash 3, unlike @TheBonsai's answer.
    – srcerer
    Oct 30, 2019 at 19:03
  • It even resolves variable, ((i="$first" ; i<="$last" ; i+="$step")). Not so easy with curly braces and seq.
    – Blaa_Thor
    Dec 4, 2019 at 15:15
  • This is sometimes much faster than {0..10..2}-style, e.g. when the range is really large.
    – Ruslan
    Sep 21, 2020 at 21:01
25
#!/bin/bash
for i in $(seq 1 2 10)
do
   echo "skip by 2 value $i"
done
3
  • seq is a Linux command. This won't be available on Mac OS X or FreeBSD.
    – Dereckson
    Oct 20, 2013 at 13:20
  • 6
    FWIW seq is available in my standard OS X Mavericks 10.9.1 Feb 16, 2014 at 10:53
  • I have try some other sintax, but that is the only is working for my on a little CentOS. @z - has saved my day :D
    – m3nda
    Oct 31, 2014 at 20:52
14

Use seq command:

$ seq 4
1
2
3
4

$ seq 2 5
2
3
4
5

$ seq 4 2 12
4
6
8
10
12

$ seq -w 4 2 12
04
06
08
10
12

$ seq -s, 4
1,2,3,4
3

brace expansion {m..n..s} is more efficient than seq. AND it allows a bit of output formatting:

$ echo {0000..0010..2}
0000 0002 0004 0006 0008 0010

which is useful if one numbers e.g. files and want's a sorted output of 'ls'.

1
  • Be aware that some programs may read 0010 as 8 (i.e. they read octal when there is a leading 0 because this is done in C). Jun 26 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.