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 [2010/12/28 18:12]
thebonsai [Synopsis]
syntax:ccmd:classic_for [2017/01/19 22:02] (current)
4dummies [Loop over a number range] removed a stray "1"
Line 13: Line 13:
 done done
 </​code>​ </​code>​
 +alternative,​ historical and undocumented syntax ((http://​pubs.opengroup.org/​onlinepubs/​9699919799/​xrat/​V4_xcu_chap02.html#​tag_23_02_09_12))
 <​code>​ <​code>​
-# alternative,​ historical and undocumented syntax 
- 
 for <​NAME>;​ { for <​NAME>;​ {
   <​LIST>​   <​LIST>​
Line 46: Line 44:
 } }
 </​code>​ </​code>​
-This syntax is **not documented** and should not be used. I found the parser definitions for it in 1.x code, and in modern 4.x code. My guess is that it's there for compatiblity reasons. This syntax is not specified by POSIX(R).+This syntax is **not documented** and should not be used. I found the parser definitions for it in 1.x code, and in modern 4.x code. My guess is that it's there for compatiblity reasons. This syntax is not specified by POSIX(r).
  
  
Line 127: 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 =====
Line 133: Line 151:
  
   * [[syntax:​ccmd:​c_for]]   * [[syntax:​ccmd:​c_for]]
-