Getting Familiar With The Bash For Loop Statement

One of the fundamental concepts in programming languages is looping. Loops come in handy when you need to run a series of commands repeatedly until a particular condition is met.

Loops are also useful for automating repetitive tasks, particularly in scripting languages like Bash. Basically, a ‘for loop’ is known as an iteration statement, specifically a repetition of a process within a Bash script.

Bash scripting language

Bash is a Unix shell and command language. Originally, it written as a free software replacement for the Bourne shell. Bash is also a command language interpreter widely used in different operating systems. It is typically the acronym for Bourne-Again Shell.

Bash scripts can be used for different purposes, including the execution of a shell command, running of multiple commands together, customization of administrative tasks, and automation of tasks, among others.

For a Linux user, basic knowledge of Bash programming is essential.

Standard Bash for loop

For loop will iterate a list of items and perform a set of commands. The list of items can be a series of strings separated by spaces, range of numbers, output of a command, or array elements.

  • Loop over strings

This type of loop will iterate over every item in the list of strings. For instance, if you have a series of string elements under a particular variable, the for loop will iterate each item on the list as specified in the statement.

  • Loop over a number range

If you want to specify a range of numbers or characters, you can use the sequence expression by defining a start and end point of the range. A sample syntax is {start..end} wherein you have to specify a number or character to begin and another to end.

So if you are going to assign a particular number to begin, like 0, and end it with a number 3. Your output will increment until the starting number reaches the value specified in your loop. In this case, 0, 1, 2, and 3.

You can also specify an increment when you are using ranges. The expression will take the form {start..end..increment} so if your loop starts at 0 and ends in 20, and your increment is 5, then your output will increment from value 0-20 by 5. In this case, 0, 5, 10, 15, and 20.

  • Loop over array elements

An array of elements can also be iterated using a for loop statement. Just like in the ‘for loop’ statement used for strings, you can use it for your array elements. This means that more than one word can be displayed separated by spaces.

  • C-style Bash for loop

The ‘for loop’ statement is composed of the Initialization, Test, and Step values, following the form ((Initialization; Test; Step)). The Initialization part is executed once after the loop starts.

Then, the Test part will be evaluated and if it is false, the loop will be terminated. If the Test part is true, the commands in the body of the ‘for loop’ statement will be executed. Likewise, the Step part will be updated. In this case, the form ((i=0; i<= 10; i++)) will iterate 11 times.

Overall, the Bash ‘for loop’ will execute a set of commands for a fixed number of times in a repeated manner.

Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post

RAID 5 vs RAID 10: Which Level Is More Advantageous?

Next Post

OnApp Federation Grows Stronger with SolusVM Acquisition

Related Posts