Using Bash For Loops to Iterate Over a List of Strings
Last updated
Last updated
When working with text files or processing text data, very often you will find yourself working with a list of strings. Text processors use looping methods to iterate over the list and access the individual string elements. In this guide, we will learn how to use for loop in bash to iterate over a list of strings.
We can use the for loop to loop through individual words in a string.
See the following example:
#!/bin/bash
str="Using for loops for string iteration"
for
i in
$str; do echo
"Word: $i";done
We declare a string variable and pass it to for loop using an iterator.
The variable iterates through the string and holds the individual words until no more can be processed.
When working with a list of strings, each string element could either be a single word or multiple space-separated words. Let’s see how we can use for loop in both cases.
We can use the same for-loop syntax for iterating over a list containing multiple strings.
Example:
#!/bin/bash
for
i in
"Using"
"for"
"loops"
"for"
"string iteration"; do echo
"String: $i";done
We pass the list of string elements to the for loop using an iterator.
The iterator loops through the list elements and holds the individual string variables. Note that the last element of the list is a multi-word string and the iterator references this entire string.
Let’s see how we can use nested loops to access the individual elements in a list containing multi-word strings.
Using the previous example:
#!/bin/bash
for
i in
"Using"
"for"
"loops"
"for"
"string iteration"; do for
j in
$i; do echo
"String: $j"; donedone
The outer for loop access the individual elements of the list.
The inner loop iterates over the space-separated words in the list elements.
We see the words “string”, and “iteration” are printed as individual elements in this example.
When working with arrays, we can use the special index variable ‘@’ to iterate over the individual string elements.
#!/bin/bash
declare
-a stringArr=("Using"
"for"
"loops"
"for"
"string iteration")
for
i in
"${stringArr[@]}"; do echo
"String: $i";done
The iterator holds the value of the individual list elements until no more can be processed. If an element is a multi-word string, then the variable references this entire string.
In the previous example, we enclosed ${stringArr[@]} in double quotes. When using the double quotes, the iterator references the individual list elements.
Let’s see what happens when we use this syntax without the double quotes.
#!/bin/bash
declare
-a stringArr=("Using"
"for"
"loops"
"for"
"string iteration")
for
i in
${stringArr[@]}; do echo
"String: $i";done
This time we use the ‘@’ index syntax without the double quotes.
Using this syntax, for loop splits the multi-word strings and the individual words can be referenced.
Bash scripting also supports C-Style for loop syntax.
Let’s take an example to understand this further:
#!/bin/bash
declare
-a stringArr=("Using"
"for"
"loops"
"for"
"string iteration")size=${#stringArr[@]}
for((i=0; i<$size; i++)); do echo
"String: ${stringArr[$i]}"done
We declare an array of strings and store its size in a variable.
In the for loop, we initialize the index variable i = 0 and increment it in the loop as long as i < size of the array.
The individual array elements can be accessed using the array-index referencing syntax.
In the case of multi-word strings, this considers the entire string as a single element. If we want to access the individual words in the string, we need another for loop to iterate over the string.
Bash by default considers space (‘ ‘) as a delimiter. If we want to use some other character as a delimiter, it can be controlled by a special shell variable called internal field separator (IFS).
Let’s take an example to understand this further:
#!/bin/bash
str="Using-for loops-for-string-iteration-using a-custom delimiter"
echo
"Before assigning IFS='-'"for
i in
$str; do echo
"String: $i"done
IFS='-'
echoecho
"After assigning IFS='-'"for
i in
$str; do echo
"String: $i"done
We have a string variable that has a few words separated by (‘-’) and a few separated by space (‘ ‘).
Let’s see the output of for loop iterating through this string variable before and after setting the IFS variable.
In the first for loop, we have the default value of IFS variable. Thus, Bash treats space as the delimiter.
We declare an and use the for loop to iterate over this array using the special index variable ‘@’.
When IFS to any other character, Bash starts treating it as the delimiter.
After setting IFS=‘-’, Bash based on dash (‘-’) character while considering the space (‘ ‘) separated words as a single element.