November 2015
Beginner
282 pages
5h 5m
English
The for loop can be used to iterate over the items in a list or till the condition is true.
The syntax of using the for loop in bash is as follows:
for item in [list] do #Tasks done
Another way of writing the for loop is the way C does, as follows:
for (( expr1; expr2; expr3 )) # Tasks done
Here, expr1 is initialization, expr2 is condition, and expr3 is increment.
The following shell script explains how we can use the for loop to print the values of a list:
#!/bin/bash
# Filename: for_loop.sh
# Description: Basic for loop in bash
declare -a names=(Foo Bar Tom Jerry)
echo "Content of names array is:"
for name in ${names[@]}
do
echo -n "$name "
done
echoThe output of the script is as follows:
Content of names array ...
Read now
Unlock full access