<p>I'm looking for the basic loop like:</p>
<pre><code>for(int i = 0; i &lt; MAX; i++) {
  doSomething(i);
}
</code></pre>
<p>but for Bash.</p>

[{'answer_id': 49111, 'author': 'Rob Rolnick', 'author_id': 4798, 'author_profile': 'https://Stackoverflow.com/users/4798', 'pm_score': 8, 'selected': True, 'text': '<p>From <a href="http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html" rel="noreferrer">this site</a>:</p>\n\n<pre><code>for i in $(seq 1 10);\ndo\n    echo $i\ndone\n</code></pre>\n'}, {'answer_id': 49114, 'author': 'Nick Stinemates', 'author_id': 4960, 'author_profile': 'https://Stackoverflow.com/users/4960', 'pm_score': 6, 'selected': False, 'text': '<pre><code>for ((i = 0 ; i &lt; max ; i++ )); do echo "$i"; done\n</code></pre>\n'}, {'answer_id': 49115, 'author': 'Pat Notz', 'author_id': 825, 'author_profile': 'https://Stackoverflow.com/users/825', 'pm_score': 4, 'selected': False, 'text': '<p>Try the Bash built-in help:</p>\n<pre class="lang-none prettyprint-override"><code>help for\n</code></pre>\n<h3>Output</h3>\n<pre class="lang-none prettyprint-override"><code>for: for NAME [in WORDS ... ;] do COMMANDS; done\n    The `for\' loop executes a sequence of commands for each member in a\n    list of items.  If `in WORDS ...;\' is not present, then `in &quot;$@&quot;\' is\n    assumed.  For each element in WORDS, NAME is set to that element, and\n    the COMMANDS are executed.\n\nfor ((: for (( exp1; exp2; exp3 )); do COMMANDS; done\n    Equivalent to\n        (( EXP1 ))\n        while (( EXP2 )); do\n            COMMANDS\n            (( EXP3 ))\n        done\n    EXP1, EXP2, and EXP3 are arithmetic expressions.  If any expression is\n    omitted, it behaves as if it evaluates to 1.\n</code></pre>\n'}, {'answer_id': 49125, 'author': 'Fernando Barrocal', 'author_id': 2274, 'author_profile': 'https://Stackoverflow.com/users/2274', 'pm_score': 5, 'selected': False, 'text': "<p>The Bash <code>for</code> consists on a variable (the iterator) and a list of words where the iterator will, well, iterate.</p>\n<p>So, if you have a limited list of words, just put them in the following syntax:</p>\n<pre><code>for w in word1 word2 word3\ndo\n  doSomething($w)\ndone\n</code></pre>\n<p>Probably you want to iterate along some numbers, so you can use the <code>seq</code> command to generate a list of numbers for you: (from 1 to 100 for example)</p>\n<pre><code>seq 1 100\n</code></pre>\n<p>and use it in the <em>for</em> loop:</p>\n<pre><code>for n in $(seq 1 100)\ndo\n  doSomething($n)\ndone\n</code></pre>\n<p>Note the <code>$(...)</code> syntax. It's a Bash behaviour, and it allows you to pass the output from one command (in our case from <code>seq</code>) to another (the <code>for</code>).</p>\n<p>This is really useful when you have to iterate over all directories in some path, for example:</p>\n<pre><code>for d in $(find $somepath -type d)\ndo\n  doSomething($d)\ndone\n</code></pre>\n<p>The possibilities are infinite to generate the lists.</p>\n"}, {'answer_id': 49180, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>I use variations of this all the time to process files...</p>\n\n<blockquote>\n  <p>for files in *.log; do echo "Do stuff with: $files"; echo "Do more stuff with: $files"; done;</p>\n</blockquote>\n\n<p>If processing lists of files is what you\'re interested in, look into the <strong>-execdir</strong> option for <strong>files</strong>.</p>\n'}, {'answer_id': 77973, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>If you\'re interested only in Bash, the &quot;for(( ... ))&quot; solution presented above is the best, but if you want something <a href="https://en.wikipedia.org/wiki/POSIX" rel="nofollow noreferrer">POSIX</a> SH compliant that will work on all Unices, you\'ll have to use &quot;expr&quot; and &quot;while&quot;, and that\'s because &quot;(())&quot; or &quot;seq&quot; or &quot;i=i+1&quot; are not that portable among various shells.</p>\n'}, {'answer_id': 143376, 'author': 'terson', 'author_id': 22974, 'author_profile': 'https://Stackoverflow.com/users/22974', 'pm_score': 2, 'selected': False, 'text': "<p>I commonly like to use a slight variant on the standard <em>for</em> 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 <em>for</em> loops.</p>\n<p>Example:</p>\n<p>I want to run the uptime command on frontend hosts 1-5 and backend hosts 1-3:</p>\n<pre><code>% for host in {frontend{1..5},backend{1..3}}.mycompany.com\n    do ssh $host &quot;echo -n $host; uptime&quot;\n  done\n</code></pre>\n<p>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.</p>\n"}, {'answer_id': 3307917, 'author': 'David Gardner', 'author_id': 86080, 'author_profile': 'https://Stackoverflow.com/users/86080', 'pm_score': 5, 'selected': False, 'text': '<p>Bash <em>3.0+</em> can use this syntax:</p>\n<pre><code>for i in {1..10} ; do ... ; done\n</code></pre>\n<p>...which avoids spawning an external program to expand the sequence (such as <code>seq 1 10</code>).</p>\n<p>Of course, this has the same problem as the <code>for(())</code> solution, being tied to Bash and even a particular version (if this matters to you).</p>\n'}, {'answer_id': 14098446, 'author': 'Brent Bradburn', 'author_id': 86967, 'author_profile': 'https://Stackoverflow.com/users/86967', 'pm_score': 3, 'selected': False, 'text': '<pre><code>#! /bin/bash\n\nfunction do_something {\n   echo value=${1}\n}\n\nMAX=4\nfor (( i=0; i&lt;MAX; i++ )) ; {\n   do_something ${i}\n}\n</code></pre>\n\n<hr>\n\n<p>Here\'s an example that can also work in older shells, while still being efficient for large counts:</p>\n\n<pre><code>Z=$(date) awk \'BEGIN { for ( i=0; i&lt;4; i++ ) { print i,"hello",ENVIRON["Z"]; } }\'\n</code></pre>\n\n<p>But good luck doing useful things inside of <code>awk</code>: <a href="https://stackoverflow.com/q/19075671/86967">How do I use shell variables in an awk script?</a></p>\n'}]
['https://Stackoverflow.com/questions/49110', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3535/']