
What is a bash For Loop?
As a Linux System Administrator there will come times where a task can be made much simpler and faster to complete with the use of For Loops in bash.
A bash For Loop is a script or statement where you have a set of items (i.e a list of items within a file) and you DO an action on each of those items repeatedly until each have been addressed and the loop will then end.
Why use bash For Loops?
With For Loops in bash you can execute a script continuously on a large number of items within a file to automate repetitive tasks which normally would take a long period of time to complete if done manually.
For example, there could be a use case where you need to import a large list of databases into MySQL/MariaDB but the list contains hundreds or thousands of databases.
In that same example, with bash For Loops you could create a list of databases within a simple txt file and craft a For Loop script to execute a creation statement in MySQL/Mariadb for each using the txt file and then import them from a backup directory into MySQL/MariaDB.
Through out this article we will explain the basic syntax of bash For Loops as well as show some loop examples to help get you started.
General syntax of a bash For Loop
The general syntax of a For Loop statement is as follows:
for <variable name> in <a list of items>;do <some command> $<variable name>;done;

Or to simplify the statement even further:
for <item> in <a list>; do <an action on items in list>;done;
Quite commonly we will use ‘i’ as a placeholder for variables such as we see in the following syntax example:
for i in <a list of items>;do <some command> $i;done;
If our “list of items” are all on separate lines within a file then the ‘i’ variable would just represent each individual lines item.
The “list of items” can be a file or path to a file where a bunch of items are listed and then we “do” some command on each of those items.
For the “some command” part of the command, this can be any command you’re needing to execute on the list of items.
Once the loop starts it will go through each item within a list and once all of the items have been addressed the loop terminates.
For Loop examples
Now that we’ve gone over the basics of bash For Loops and its syntax, lets look at some real use cases to gain an even better grasp on the scripting.
Example 1: Running a For Loop bash script on a list of items in a file
Expanding off from the basics of a bash For Loop script, in the following example lets say we have a list of items in a file, such as domains, and we want to run a command on each domain name within that file.
If we wanted to run a ‘dig’ command on each of the domains listed in the file (our variables in this case) we would use something like in the following For Loop example:
for i in `cat test-file.txt`; do echo;echo $i;dig +short ns $i;done

From this For Loop statement we are saying, “For each item/variable listed in test-file.txt ‘echo’ the name of the item and output its name then do a ‘dig’ command on each” and output those results.
This same script can also be written like in the following statement:
for i in $(cat test-file.txt); do echo;echo $i;dig +short ns $i;done
So in this case our file will contain this list of domains which are the variables:

After the loop runs on each of the items within the file it will echo/output the following information provided by the ‘dig’ command from the DO section of the script:

We see that from above we listed each item in test-file.txt and completed a ‘dig +short ns’ on them to obtain the Name Servers in use for each of the domains.
Example 2: Using a For Loop bash script to create databases from a list
Lets say we have a list of databases we need to add and we’d like to do so using a For Loop.
Below we see we have our list of database names within /home/temp/database-name.txt:

From the above list we will craft a For Loop statement to add a new database for each of them within MySQL/MariaDB.
So far we have this part of the For Loop ready which lists each item (database name) within the given file:
for i in $(cat /home/temp/database-names.txt)
Now we just need to craft the DO section in order to perform a creation all the databases based off our variables which in this case is set to ‘i’ and will be the database names:
do mysqladmin create $i
When we put them together the entire For Loop statement syntax would look like this:
for i in $(cat /home/temp/database-names.txt); do mysqladmin create $i;done
Now that we have each part of the For Loop crafted lets put them together, perform the bash For Loop script and check our work:

- As we can see from the above we executed the For Loop bash script to obtain a list of database names from a file using the ‘cat’ command.
- Then with those database names we created a database in MySQL/MariaDB for each item in the list and the loop ends.
- After checking our work with the ‘mysqlshow’ statement on the command line we see each of the databases were added successfully.
Example 3: Using a For Loop bash script to complete 2 actions
Expanding even further from the Example 2 section, you may come across some instances where you would need to have two commands executed inside of a single For Loop bash script on the command line.
In the next following example lets say that our /var/lib/mysql data directory has been wiped out and we need to recreate every single database and then also import mysqldump backups of each database back into MySQL/MariaDB.
Much like in example 2, in the below we have a list of database names within /home/temp/database-names.txt and we have mysqldump (.sql files) copies of each database within /home/temp/db_backup.
With that information what we will do is craft a For Loop statement to do the following:
- Take each item (database name) listed within our database-names.txt file
- Create a new database in MySQL/MariaDB based off each item (database name) in our database-names.txt file
- Recover/import each of those databases from a mysqldump file located in /home/temp/db_backup
Such a For Loop would look like the following:
for i in $(cat /home/temp/database-names.txt); do echo;echo Importing $i;mysqladmin create $i;mysql $i < /home/temp/db_backup/${i}.sql;done

From the above For Loop bash statement we are completing the following:
- Executing a ‘cat’ command to obtain the list of databases from a txt file as our list of items and each item is our ‘i’ variable.
- Afterward we echo the output while creating each database in MySQL/MariaDB from the list of database names in /home/temp/database-names.txt
- Then we are importing each of the mysqldump backups into the appropriate database which was created.
This is something I’ve personally had to do on a multiple occasions and being able to automate the task made things much easier.
When you’re dealing with over 80 databases this could take hours if done manually.
Incorporate Array Elements with For Loops
Arrays can be used to organize data and with the use of For Loops we can execute code on each element within an array.
For Loops can be a helpful tool if you are needing to execute certain commands on an array of filenames or elements within a file.
The basic idea of using a For Loop on an array is to say “For each element in an array, Do a specific action”.
Within this next section we will cover the two types of array in bash and basic examples for each.
Indexed Arrays
An indexed array is the simplest of the two types where elements are assigned to an numerical index based off their position in the array such as in the following example:
candy=("Snickers" "Skittles" "Reeses")
Within an indexed array elements will start with the position of 0 and progress from there.
As example from the above sample array we see an array called “candy” and there are 3 types of elements.
Within the above array the “Snickers” element would hold position 0, “Skittles” holds position 1, and “Reeses” holds position 2.
If we were to create a For Loop for the above sample array named “candy” and echoed each elements name within the array as our action it would look like this:
#!/bin/bash
# Array
candy=("Snickers" "Skittles" "Reeses")
# For Loop for Array
for candy in "${candy[@]}"
do
echo "$candy"
done

After executing the above we see that the For Loop iterates through the array elements and will show the following output:

Associative Array
When it comes to associative arrays they will use key values instead of numerical values for an elements position within the array.
When crafting an associative array we will first need to declare the array with a declare statement:
declare -A
Following the declare statement we would then define the array which in this example will once again be a list of different candies (as the key) and the company name (as the value) for who manufacturers each candy:
candy_company=(["Snickers"]="Mars" ["Skittles"]="MarsWrigley" ["Reeses"]="Hershey")
Now that we have our declare statement and the array put together we can create a For Loop to iterate through the elements in the array:
for key in "${!candy_company[@]}"
do
echo "Key: $key, Value: ${candy_company[$key]}"
done

After executing this For Loop in our example file we would see the following output:

Conclusion
The ability to automate jobs and procedures with the use of bash scripting can be a crucial skill to have in any Linux Administrators tool belt.
Within this article we went over the basics of bash For Loops and how they make procedures (which would normally take hours to complete if done manually) a much easier and faster undertaking.
Being able to familiarize yourself with For Loops is essential for success and will save you countless hours when tedious procedures arise.
Leave a Reply