Bash Loop Through a List of Strings

29/12/2020
A list of strings or array or sequence of elements can be iterated by using for loop in bash. How you can iterate the list of strings in Bash by for loop is shown in this tutorial by using various bash script examples. If you are novice is bash programming then you can read the tutorial on BASH For Loop Examples before starting this tutorial.

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.

#!/bin/bash
# Read a string with spaces using for loop
for value in I like programming
do
    echo $value
done

Output:

$ bash for_list1.sh

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.

#!/bin/bash
# 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:

$ bash for_list2.sh

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.

#!/bin/bash
 
# 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:

$ bash for_list3.sh

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.

#!/bin/bash
 
# 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:

$ bash for_list4.sh

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.

#!/bin/bash
 
#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:

$ bash for_list5.sh

 

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.

#!/bin/bash
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:

$ bash for_list6.sh

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.

#! /bin/sh
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:

$ bash for_list7.sh

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.

#! /bin/sh
 
# 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:

$ bash for_list8.sh

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:

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Some Useful Bash Aliases and How to Create Bash Aliases

Do you spend a good amount of time working in the command line? Then you may have noticed that most of the commands you...
29/12/2020

BASH While Loop Examples

Three types of loops are used in bash programming. While loop is one of them. Like other loops, while loop is used to do...
28/12/2020

NFS Server and Client Installation on CentOS 7

1 Preliminary Note I have fresh installed CentOS 7 server, on which I am going to install the NFS server. My CentOS server...
01/11/2021
Bài Viết

Bài Viết Mới Cập Nhật

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024

Dịch vụ thuê mua proxy giá rẻ an toàn, tốc độ cao
13/05/2024

Thuê mua proxy V6 uy tín, chất lượng tại đâu?
11/05/2024

Thuê mua proxy Tiktok tăng doanh thu, hiệu quả cao
11/05/2024