Example-1: Iterating a string of multiple words within for loop
Create a bash file named ‘for_list1.sh’ and add the following script. A string value with spaces is used within for loop. By default, string value is separated by space. For loop will split the string into words and print each word by adding a newline.
# Read a string with spaces using for loop
for value in I like programming
do
echo $value
done
Output:
Example-2: Iterating a string variable using for loop
Create a bash file named ‘for_list2.sh’ and add the following script. Assign a text into the variable, StringVal and read the value of this variable using for loop. This example will also work like the previous example and divide the value of the variable into words based on the space.
# Define a string variable with a value
StringVal="Welcome to linuxhint"
# Iterate the string variable using for loop
for val in $StringVal; do
echo $val
done
Output:
Example-3: Iterate an array of string values
Create a bash file named ‘for_list3.sh’ and add the following script. An array of string values is declared with type in this script. Two values in the array which contain space are “Linux Mint” and “Red Hat Linux”. This script will generate the output by splitting these values into multiple words and printing as separate value. But this is not the proper output. The solution of this type of problem is shown in the next example.
# Declare an array of string with type
declare -a StringArray=("Linux Mint" "Fedora" "Red Hat Linux" "Ubuntu" "Debian" )
# Iterate the string array using for loop
for val in ${StringArray[@]}; do
echo $val
done
Output:
Example-4: Print multiple words string value as a single value
Create a bash file named ‘for_list4.sh’ and add the following script. In this example, every element of the array variable, StringArray contains values of two words. To print each value without splitting and solve the problem of previous example, you just need to enclose the array variable with double quotation within for loop.
# Declare a string array with type
declare -a StringArray=("Windows XP" "Windows 10" "Windows ME" "Windows 8.1"
"Windows Server 2016" )
# Read the array values with space
for val in "${StringArray[@]}"; do
echo $val
done
Output:
Example-5: Iterating string values of an array using ‘*’
Create a bash file named ‘for_list5.sh’ with the following code. Here, ‘*’ symbol is used to read all string values of the array. The first for loop is used to display array values in multiple lines and the second for loop is used to display array values in a single line.
#Declare a string array
LanguageArray=("PHP" "Java" "C#" "C++" "VB.Net" "Python" "Perl")
# Print array values in lines
echo "Print every element in new line"
for val1 in ${LanguageArray[*]}; do
echo $val1
done
echo ""
# Print array values in one line
echo "Print all elements in a single line"
for val2 in "${LanguageArray[*]}"; do
echo $val2
done
echo ""
Output:
Example-6: Iterating comma separated string values
Create a new bash file named ‘for_list6.sh’ with the following code. Here, comma (,) is used to split the string values. IFS variable is used to set the field separator.
DataList=" HTML5, CCS3, BootStrap, JQuery "
Field_Separator=$IFS
# set comma as internal field separator for the string list
IFS=,
for val in $DataList;
do
echo $val
done
IFS=$Field_Separator
Output:
Example-7: Reading multiple string arrays together
Create a bash file named ‘for_list7.sh’ and add the following script. In this example, two string arrays are defined and combined into another array. The outer for loop is used to read the combined array and the inner for loop is used to read each inner array.
str_array1=("Magento 2.2.4" "WooCommerce")
str_array2=("CodeIgnitor" "Laravel")
combine=(str_array1 str_array2)
for arrItem in ${combine[@]}
do
eval ‘for val in "${‘$arrItem‘[@]}";do echo "$val";done’
done
Output:
Example-8: Using pattern to read the list of strings
Create a new bash file named for_list8.sh with the following code. Here, ‘/, /’ pattern is used to split the string values based on comma.
# Define a list of string variable
stringList=WordPress,Joomla,Magento
# Use comma as separator and apply as pattern
for val in ${stringList//,/ }
do
echo $val
done
Output:
Hope, the examples of this tutorial will help you to understand the use of for loop for iterating the list of strings, for a video on this topic, see below: