Linux

Know About Bash for Loop in Linux with Examples

[ad_1]

How to make automation with bash for loop under Linux/UNIX operating system? How do I use break, continue and the parameter for loop control expression? How to act on files using for loop?

For loop is a very useful tool to solve many problems in the programming world and therefore we will solve some problems in the real world. In Linux we use loops via Bash, Python to make automation like password script, counting script. In this article, we will explain all of the kind of loops for Bash.

1) for loop

We can use For loop to read all elements in a list or part of them and displaying these elements on the screen.

Syntax of For loop

for variable in element1 element2 element3 Or <List>
do
commands
done

We will make a script to send greeting message for each day of the week, so we will use for loop to read each day and print it out. Create file welcome.sh with nano editor and paste code below.

Run the welcome.sh file with bash command.

The for loop will take each item in the list respectively, and assign this item to the variable $day, after that execute the code between do and done then go back to the top, assign the next item in the list and repeat over.

We can use a range with for loop to put start point and end point.

If we want to count even number from 1 to 20, we will add step for range.

Another type of using step with for loop.

And for negative step we can use the following type.

Examples on For loop

1) Acting on files using for loop

Bash For loop is the best way when we are working on files.

2) One line For loop

we can execute For loop in one line, we will rename all *.txt files to remove the file extension.

Or in script

3) Reading Command-line arguments

When we are executing For loop script, we can enter arguments.

4) Reading odd and even number

We will write script to read the even and the odd numbers.

2) while loop

While loop depend on the condition is true, if the condition is false the interpreter get out from the loop.

Syntax of while loop

while [ <some condition> ]do
<commands>
done

We will count from 10 to 20 and print out the results. So we will put a condition that the counter less than or equal 20.

3) Until loop

Until loop like while loop but the interpreter excute the commands within it until the condition becomes true.

Syntax of until loop

until [ <some condition> ]do
<commands>
done

We will count from 10 to 20 and print out the results. So we will put a condition that the counter greater than or equal 20.

4) Controlling loops

We can use Break or Continue to control loops.

Break statement

for Break statement, we can get out from the loop and no need to complete the loop when we use if statement inside the loop.

Continue statement

For Continue statement, we can go on the loop and no need to finish the loop when we are using if statement inside the loop.

5) Select loop

Select loop like while and until loop but allows you to create a simple menu system.

Syntax of select loop

select var in <list>
do
<commands>
done

We will make three options and use select loop to choose from them.

Conclusion

  1. Bash For loop used in synchronization, making password, backup and etc…
  2. Do while is same as while but the interpreter executes the first code without any conditions
  3. Break statement is very important for getting out from the loop
  4. Select statement is useful when we have many options

Read Also:

[ad_2]

Facebook Comments

Show More

Related Articles

Leave a Reply

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

Back to top button