For loop is a structure that executes a number of times, generally known in advance. In Bash script syntax of FOR loop is:
for VARIABLE [in LIST]; do COMMANDS BLOCK; done
or
for VARIABLE [in LIST]
do
COMMANDS_BLOCK
done
LIST is a list of words or numbers. If LIST is omitted $@ is implied.
Simple examples:
1. This example prints all files in directory
dirlist=$(ls)
for i in $dirlist
do
echo $i
done
2. This example prints numbers from 1 to 10
for i in {1..10}
do
echo $i
done
or
for i in 1 2 3 4 5 6 7 8 9 10
do
echo $i
done
3. This example prints number from 1 to 10 with step 2
for i in {1..10..2}
do
echo $i
done
4. This example prints
for VARIABLE [in LIST]; do COMMANDS BLOCK; done
or
for VARIABLE [in LIST]
do
COMMANDS_BLOCK
done
LIST is a list of words or numbers. If LIST is omitted $@ is implied.
Simple examples:
1. This example prints all files in directory
dirlist=$(ls)
for i in $dirlist
do
echo $i
done
2. This example prints numbers from 1 to 10
for i in {1..10}
do
echo $i
done
or
for i in 1 2 3 4 5 6 7 8 9 10
do
echo $i
done
3. This example prints number from 1 to 10 with step 2
for i in {1..10..2}
do
echo $i
done
4. This example prints
No comments:
Post a Comment