137

I'm looking for the basic loop like:

for(int i = 0; i < MAX; i++) {
  doSomething(i);
}

but for Bash.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
John Meagher
  • 22,808
  • 14
  • 54
  • 57
  • 2
    possible duplicate of [How do I iterate over a range of numbers defined by variables in bash?](http://stackoverflow.com/questions/169511/how-do-i-iterate-over-a-range-of-numbers-defined-by-variables-in-bash) – Amir Ali Akbari Oct 21 '13 at 10:33

9 Answers9

152

From this site:

for i in $(seq 1 10);
do
    echo $i
done
stkent
  • 19,772
  • 14
  • 85
  • 111
Rob Rolnick
  • 8,519
  • 2
  • 28
  • 17
  • 21
    It's worth a mention that the range specified here is **inclusive**. By that, I mean you will see the entire range (1 to 10) printed to the console. – Jamie Feb 22 '16 at 22:28
84
for ((i = 0 ; i < max ; i++ )); do echo "$i"; done
Chris Down
  • 1,590
  • 1
  • 14
  • 25
Nick Stinemates
  • 41,511
  • 21
  • 59
  • 60
  • if we have a variable like `ArrayLength=${#array[@]}` how do we use it in here instead of `max`? – Kasun Siyambalapitiya Dec 02 '16 at 10:44
  • I prefer this solution since it does not use a `seq` so it does not generate a weird string containing a sequence of all the numbers, but it just loops efficiently over an integer. – Valerio Bozz Mar 21 '23 at 15:25
48

The Bash for consists on a variable (the iterator) and a list of words where the iterator will, well, iterate.

So, if you have a limited list of words, just put them in the following syntax:

for w in word1 word2 word3
do
  doSomething($w)
done

Probably you want to iterate along some numbers, so you can use the seq command to generate a list of numbers for you: (from 1 to 100 for example)

seq 1 100

and use it in the for loop:

for n in $(seq 1 100)
do
  doSomething($n)
done

Note the $(...) syntax. It's a Bash behaviour, and it allows you to pass the output from one command (in our case from seq) to another (the for).

This is really useful when you have to iterate over all directories in some path, for example:

for d in $(find $somepath -type d)
do
  doSomething($d)
done

The possibilities are infinite to generate the lists.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fernando Barrocal
  • 12,584
  • 9
  • 44
  • 51
43

Bash 3.0+ can use this syntax:

for i in {1..10} ; do ... ; done

...which avoids spawning an external program to expand the sequence (such as seq 1 10).

Of course, this has the same problem as the for(()) solution, being tied to Bash and even a particular version (if this matters to you).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
David Gardner
  • 6,952
  • 4
  • 35
  • 37
18

Try the Bash built-in help:

help for

Output

for: for NAME [in WORDS ... ;] do COMMANDS; done
    The `for' loop executes a sequence of commands for each member in a
    list of items.  If `in WORDS ...;' is not present, then `in "$@"' is
    assumed.  For each element in WORDS, NAME is set to that element, and
    the COMMANDS are executed.

for ((: for (( exp1; exp2; exp3 )); do COMMANDS; done
    Equivalent to
        (( EXP1 ))
        while (( EXP2 )); do
            COMMANDS
            (( EXP3 ))
        done
    EXP1, EXP2, and EXP3 are arithmetic expressions.  If any expression is
    omitted, it behaves as if it evaluates to 1.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pat Notz
  • 208,672
  • 30
  • 90
  • 92
5
#! /bin/bash

function do_something {
   echo value=${1}
}

MAX=4
for (( i=0; i<MAX; i++ )) ; {
   do_something ${i}
}

Here's an example that can also work in older shells, while still being efficient for large counts:

Z=$(date) awk 'BEGIN { for ( i=0; i<4; i++ ) { print i,"hello",ENVIRON["Z"]; } }'

But good luck doing useful things inside of awk: How do I use shell variables in an awk script?

Brent Bradburn
  • 51,587
  • 17
  • 154
  • 173
  • 3
    I didn't notice it before, but a similar syntax is shown in one of the earlier answers. The unique thing here is the use of curly-braces instead of the typical `do`/`done` pair. – Brent Bradburn Jun 21 '13 at 15:48
4

I commonly like to use a slight variant on the standard for loop. I often use this to run a command on a series of remote hosts. I take advantage of Bash's brace expansion to create for loops that allow me to create non-numerical for loops.

Example:

I want to run the uptime command on frontend hosts 1-5 and backend hosts 1-3:

% for host in {frontend{1..5},backend{1..3}}.mycompany.com
    do ssh $host "echo -n $host; uptime"
  done

I typically run this as a single-line command with semicolons on the ends of the lines instead of the more readable version above. The key usage consideration are that braces allow you to specify multiple values to be inserted into a string (e.g. pre{foo,bar}post results in prefoopost, prebarpost) and allow counting/sequences by using the double periods (you can use a..z, etc.). However, the double period syntax is a new feature of Bash 3.0; earlier versions will not support this.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
terson
  • 1,307
  • 1
  • 9
  • 10
  • Related to this, what if $host var is an empty line by some accident? It would still fire ssh. So, how do I avoid that? In my case I'm doing something different and so your answer will help me. I'm trying to check Gmail for new messages, and if found, send an SMS of from and subject. My question is labeled "Connecting Two Bash Commands" if you want to read it. – Volomike Oct 25 '09 at 22:07
  • I don't believe you would get an empty $host var. This is because the example above is using brace expansion. The for loop explicitly sets the $host variable to values: frontend1.mycompany.com frontend2.mycompany.com . . backend1.mycompany.com But if you see a way for it to have a null value; I'd be interested to know. You could execute the ssh inside a conditional test if it was a concern. – terson Nov 08 '09 at 19:07
1

If you're interested only in Bash, the "for(( ... ))" solution presented above is the best, but if you want something POSIX SH compliant that will work on all Unices, you'll have to use "expr" and "while", and that's because "(())" or "seq" or "i=i+1" are not that portable among various shells.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

I use variations of this all the time to process files...

for files in *.log; do echo "Do stuff with: $files"; echo "Do more stuff with: $files"; done;

If processing lists of files is what you're interested in, look into the -execdir option for files.