Guide and examples of the for loop in Bash (Bash For Loop)

A for loop is one of the main statements in many programming languages. Here, we’ll explain how it’s used in the , hence the name, “bash for loop – bash for loop”. Get ready to add a new tool to your developer arsenal!

A for loop is an iteration statement, which means that you can execute code repeatedly. Let’s say you want to execute an instruction 5 times. Instead of writing five separate pieces of code, you can write a single for loop syntax. Let’s get into details, okay?

for loop syntax for bash

Basically, the simplest Bash for() loop syntax repeats the occurrence of a set of a variable. The syntax generally looks like this:

for VARIABLE in 1 2 3 4 5 .. N Run the following command: command1 command2 commandN done

In the real world, this syntax would look like the following example:

#!/bin/bash for i in 1 2 3 4 5 do echo “Hello $i” done

Running the bash file outputs the following text:

Hello 1 Hello 2 Hello 3 Hello 4 Hello 5

Let’s inspect each element:

  • #!/bin/bash: shows that the code is a bash script
  • Yo: is a placeholder for a variable. For its part, $i is the individual value of the variable. You can also write it as c/$c or under any other name
  • in: separates the variable and the elements that follow
  • 1 2 3 4 5: is an example of elements on which you want to perform the instruction
  • do: is the keyword that starts the loops. It will execute the statement n times, where n is the total number of elements. Here, the value of n is 5
  • echo “Hello: $i”: is the code that we will repeat n times. Remember that quotes turn anything inside them into a variable.
  • donate: stop the loop
See also  How to change the hostname in Ubuntu 18.04

The code can be written differently depending on the version of bash you’re running:

Bash version 3.0 or higher can shorten the range with » . . «.

#!/bin/bash for i in {1. .5} do echo “Hai $i” done

Bash version 4.0 or higher allows you to use the syntax {START. .END. .INCREMENT}.

#!/bin/bash for i in {0. .8. .2} do echo “Hai $i” done

The result will look like this:

Hai 0 Hai 2 Hai 4 Hai 6 Hai 8

The other two common syntaxes are:

for VARIABLE in file1 file2 file3 do command1 on $VARIABLE command2 commandN done

Or this way:

for OUTPUT in $(Linux-Or-Unix-Command-Here) do command1 on $OUTPUT command2 on $OUTPUT commandN done

for loop examples for bash

You can update the syntax to perform multiple operations. Remember that before doing anything you must log in to your server. If you have any problem, this will put you on the right path. In the meantime, if you’re having trouble with bash, you should check out our guide to basic bash functions. Remember that bash functions must be in a file .sh. To create one, run the following on the command line:

vim FileName.sh

This will create a .sh file and open it in the VIM editor. You can learn more in the bash basics article above.

Using for loop to create an infinite loop

Once activated, this loop will continue to execute code until you stop it by pressing Control + C. In this case, the phrase “Hello world” will continue to appear by itself.

#!/bin/bash for (( ; ; )) do echo “Hello world!” donate

Using Bash For Loop to create a loop of three expressions

This loop is made up of three typed expressions: an initializer (EXP1), one condition (EXP2) and a count expression (EXP3). Sometimes people call it the C-style loop because of the close resemblance in code structure. The syntax of this loop is as follows:

See also  What is it and how to fix Error 500 (Internal Server Error) in WordPress

for (( EXP1; EXP2; EXP3 )) do command1 command2 command3 done

Here is an example you can try yourself:

#!/bin/bash for (( c=1; c<=5; c++ )) do echo "Hai $c" done

The code says that the initial value is 1. The loop will execute as long as the condition in EXP2 be true, which means it must not be greater than 5. Furthermore, the sign ++ shows that the increase is 1 . Then it will repeat the loop one by one starting from the initial value. Result:

Hai 1 Hai 2 Hai 3 Hai 4 Hai 5

Using the for loop for Bash to create the skip and continue loop

The instruction continue skips the loop for the indicated value and continues with the next loop. This is what the syntax would look like:

for I in 1 2 3 4 5 do if then continue #Go to the next iteration of I in the loop and skip the statement fi statement done

Following the syntax we can create a skip and continue loop like this:

for i in {1..5} do if ]then continue fi echo “Hai $i4” done

The result would be:

Hai 1 Hai 2 Hai 3 Hai 5

as the value 4 matched the instruction continuethe loop did not execute the code and went on to the next value, which is 5.

Using Bash for Loop to create a conditional exit with Break Loop

This loop allows to stop the operation if it meets the established condition. This may be followed by another instruction. Here is the syntax:

for I in 1 2 3 4 5 do if then break fi statement done

Using this function, our code would look like this:

See also  About

for city in Manila Bangkok Jakarta Kuala Lumpur do if ]; then break fi echo “city: $city” done echo ‘Yes, that’s it!’

And finally the result:

city: Manila city: Bangkok Yes, that’s it!

The statement tells the loop to break when the condition is true (find the term Jakarta). It will then run the second code, which prints a text that says If that is all!

Summary

Bash for loop is great for automating repetitive tasks. In addition to the basic examples above, you can do much more. For example, you can track files and perform many other tasks. The list goes on!

All you have to do is write the syntax. The learning curve may be steep, but reading this introduction is a good start. Practice makes a master! Good luck!

Gustavo is passionate about creating websites. He focuses on the application of SEO strategies at for Spain and Latin America, as well as the creation of high-level content. When he is not applying new WordPress tricks you can find him playing the guitar, traveling or taking an online course.

Loading Facebook Comments ...
Loading Disqus Comments ...