Difference between revisions of "Bash for loop"

From thelinuxwiki
Jump to: navigation, search
Line 10: Line 10:
 
  do
 
  do
 
     echo "the i is $i"
 
     echo "the i is $i"
 +
done
 +
 +
for VARIABLE in 1 2 3 4 5 .. N
 +
do
 +
command1
 +
command2
 +
commandN
 +
done
 +
 +
OR
 +
 +
for VARIABLE in file1 file2 file3
 +
do
 +
command1 on $VARIABLE
 +
command2
 +
commandN
 +
done
 +
 +
OR
 +
 +
for OUTPUT in $(Linux-Or-Unix-Command-Here)
 +
do
 +
command1 on $OUTPUT
 +
command2 on $OUTPUT
 +
commandN
 
  done
 
  done
 
   
 
   
 
[[category:bash]]
 
[[category:bash]]

Revision as of 14:00, 26 September 2013

Example:

 for i in {1..5}
 do
   echo "Welcome $i times"
 done


portable for loop (for older bash versions)

for i in `seq 10`
do
   echo "the i is $i"
done
for VARIABLE in 1 2 3 4 5 .. N
do

command1 command2 commandN

done

OR

for VARIABLE in file1 file2 file3
do

command1 on $VARIABLE command2 commandN

done

OR

for OUTPUT in $(Linux-Or-Unix-Command-Here)
do

command1 on $OUTPUT command2 on $OUTPUT commandN

done