Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
syntax:ccmd:classic_for [2011/12/15 09:33]
ormaaj footnote
syntax:ccmd:classic_for [2017/01/19 21:02] (current)
4dummies [Loop over a number range] removed a stray "1"
Line 125: Line 125:
 done done
 </​code>​ </​code>​
 +
 +==== Loop over a number range ====
 +
 +Beginning in Bash 4, you can also use "​sequence expression"​ form of [[syntax:​expansion:​brace|brace expansion]] syntax when looping over numbers, and this form does not create leading zeroes unless you ask for them:
 +
 +<code bash>
 +# 100 numbers, no leading zeroes
 +for x in {0..99}; do
 +  echo $x
 +done
 +</​code>​
 +
 +<code bash>
 +# Every other number, width 3
 +for x in {000..99..2};​ do
 +  echo $x
 +done
 +</​code>​
 +
 +WARNING: the entire list is created before looping starts. ​ If your list is huge this may be an issue, but no more so than for a glob that expands to a huge list.
  
 ===== Portability considerations ===== ===== Portability considerations =====
  • syntax/ccmd/classic_for.1323941597.txt
  • Last modified: 2011/12/15 09:33
  • by ormaaj