Bash for: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
Line 15: Line 15:
</pre>
</pre>


Alternatively, for:


<pre>
  for i; do commands; done
</pre>


If `in words' is not present, the for command executes the commands once for each positional parameter that is set, as if `in "$@"' had been specified (see Positional Parameters below.)
''commands'' executes for each positional parameter (as if <tt>in "$@"</tt> had been specified.


Yes another alternative form:


{{{
<pre>
   
    for i in words; do commands; done
 
}}}
 
If the keyword "in" is not present, {{for}} will iterate through positional parameters.
 
Alternative form:
 
{{{
     for (( i=0; i<${#names[@]}; i++ )); do
     for (( i=0; i<${#names[@]}; i++ )); do
         local name=${names[${i}]}
         local name=${names[${i}]}
         echo "${name}"
         echo "${name}"
     done
     done
}}}
</pre>


!!!Iterating through $1, $2, $3 ...
!!!Iterating through $1, $2, $3 ...

Revision as of 23:54, 29 February 2016

External

Internal

Overview

The for built-in command expand words, and execute commands once for each member in the resultant list, with i bound to the current member.

   for i in words; do commands; done

Alternatively, for:

   for i; do commands; done

commands executes for each positional parameter (as if in "$@" had been specified.

Yes another alternative form:

    for (( i=0; i<${#names[@]}; i++ )); do
        local name=${names[${i}]}
        echo "${name}"
    done

!!!Iterating through $1, $2, $3 ...

{{{

    for i do
        echo ${i}
    done

}}}

!!!Iterating through a space separated list


{{{

   s="a b c"
   for i in ${s}
       do
           echo ${i}
       done

}}}

or

{{{

    s="a b c"
    for i in ${s}; do echo ${i}; done

}}}

Note the use of ";"


__Referenced by:__\\ [{INSERT com.ecyrd.jspwiki.plugin.ReferringPagesPlugin WHERE max=20, maxwidth=50}]