for.rst 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. for command
  2. ===========
  3. Synopis
  4. -------
  5. ::
  6. for <variable> in <items>; do <commands>; done
  7. Description
  8. -----------
  9. The for command is used to loop over a list of values and execute a series of
  10. commands for each of these.
  11. The counter variable of the loop is a shell variable. Please, keep in mind that
  12. an environment variable takes precedence over a shell variable of the same name.
  13. variable
  14. name of the counter variable
  15. items
  16. space separated item list
  17. commands
  18. commands to execute
  19. Example
  20. -------
  21. ::
  22. => setenv c
  23. => for c in 1 2 3; do echo item ${c}; done
  24. item 1
  25. item 2
  26. item 3
  27. => echo ${c}
  28. 3
  29. => setenv c x
  30. => for c in 1 2 3; do echo item ${c}; done
  31. item x
  32. item x
  33. item x
  34. =>
  35. The first line ensures that there is no environment variable *c*. Hence in the
  36. first loop the shell variable *c* is printed.
  37. After defining an environment variable of name *c* it takes precedence over the
  38. shell variable and the environment variable is printed.
  39. Return value
  40. ------------
  41. The return value $? after the done statement is the return value of the last
  42. statement executed in the loop.
  43. ::
  44. => for i in true false; do ${i}; done; echo $?
  45. 1
  46. => for i in false true; do ${i}; done; echo $?
  47. 0