> For the complete documentation index, see [llms.txt](https://morgan-bin-bash.gitbook.io/bash-scripting/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://morgan-bin-bash.gitbook.io/bash-scripting/using-bash-for-loops-to-iterate-over-a-list-of-strings.md).

# Using Bash For Loops to Iterate Over a List of Strings

### 1. Introduction

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.

### 2. Iterating over words in a string

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.

<figure><img src="https://linuxopsys.com/wp-content/uploads/2023/06/stringWords-070623-1-1024x261.png" alt="output of the script iterate using for loop over words in a string" height="261" width="1024"><figcaption></figcaption></figure>

The variable iterates through the string and holds the individual words until no more can be processed.

### 3. Loop over a list of strings

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.

#### 3.1 Iterating over string elements in a list

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.

<figure><img src="https://linuxopsys.com/wp-content/uploads/2023/06/listStrings-070623-2-1024x236.png" alt="output of the script iterate using for loop over a list containing multiple strings" height="236" width="1024"><figcaption></figcaption></figure>

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.

#### 3.2 Access individual words in a list of multi-word strings

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.

<figure><img src="https://linuxopsys.com/wp-content/uploads/2023/06/multiWordList-070623-3-1024x265.png" alt="output of the script using nested loop to access each elements in a list of multi word" height="265" width="1024"><figcaption></figcaption></figure>

We see the words “string”, and “iteration” are printed as individual elements in this example.

### 4. Iterating using special index variable (@)

When working with arrays, we can use the special index variable ‘@’ to iterate over the individual string elements.

#### 4.1 With double quotes

| `#!/bin/bash` `declare` `-a stringArr=("Using"` `"for"` `"loops"` `"for"` `"string iteration")` `for` `i in` `"${stringArr[@]}"; do    echo` `"String: $i";done` |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |

We declare an [array of strings](https://linuxopsys.com/topics/bash-looping-through-array-of-strings) and use the for loop to iterate over this array using the special index variable ‘@’.

<figure><img src="https://linuxopsys.com/wp-content/uploads/2023/06/specialIndexQuotes-070623-4-1024x238.png" alt="for loop through array using variable @ to iterate individual string elements" height="238" width="1024"><figcaption></figcaption></figure>

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.

#### 4.2 Without double quotes

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.

<figure><img src="https://linuxopsys.com/wp-content/uploads/2023/06/specialIndexNoQuotes-070623-5-1024x268.png" alt="for loop through array using variable @ index without double quotes" height="268" width="1024"><figcaption></figcaption></figure>

Using this syntax, for loop splits the multi-word strings and the individual words can be referenced.

### 5. Using c-style for loops for iteration

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.

<figure><img src="https://linuxopsys.com/wp-content/uploads/2023/06/cStyleIteration-070623-6-1024x239.png" alt="c style for loop to access individual array elements of strings" height="239" width="1024"><figcaption></figcaption></figure>

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.

### 6. Iteration over string using a custom delimiter

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).

When IFS [variable is assigned](https://linuxopsys.com/topics/assign-variable-bash) to any other character, Bash starts treating it as the delimiter.

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 (‘ ‘).&#x20;

Let’s see the output of for loop iterating through this string variable before and after setting the IFS variable.

<figure><img src="https://linuxopsys.com/wp-content/uploads/2023/06/customDelimiter-070623-7-1024x505.png" alt="output of for loop iterating through string variable setting the IFS variable." height="505" width="1024"><figcaption></figcaption></figure>

* In the first for loop, we have the default value of IFS variable. Thus, Bash treats space as the delimiter.&#x20;
* After setting IFS=‘-’, Bash [splits the string](https://linuxopsys.com/topics/bash-split-string-by-delimiter) based on dash (‘-’) character while considering the space (‘ ‘) separated words as a single element.
