How to Iterate Through an Array in a Shell Script (Bash)

Written by Zac Vineyard in Code

I was recently debugging a shell script and had a hard time remembering how to iterate through an array. So, to clarify this issue for other developers out there, I've provided a code example below. I found, too, that it was particularly important to put the items of the array in single quotes. This prevents the script from hitting a syntax error while iterating over a list of URLs, for example.

To run the script above in Ubuntu, save the code in a file with a .sh (like script.sh) extension and run it in the terminal using bash.


$ bash script.sh