How to Use the for Loop in a Linux Bash Shell Script

How to Use the for Loop in a Linux Bash Shell Script

Looping is an inherent artwork, which may make your work easier and assist you automate repetitive duties with relative ease.

Think about a state of affairs whereby you’ll want to replace a collection of numbers or textual content, and as an alternative of doing it manually, you will have the system do it for you. That is the ability of looping and the advantages it brings to the desk for you.

Loops, as a perform, can be found in virtually each programming language; Linux’s Bash is not any exception to this rule.

Here is a information explaining how you should use the for loop in a shell script.

The for Loop Construction

Utilizing the for loop in shell scripts is fairly simple, and you may manipulate the construction to realize completely different objectives.

The essential construction is as follows:

for merchandise in [LIST]
do
[COMMANDS]
carried out

With a loop, you possibly can cycle by way of numeric and character values, relying on the necessity of the hour.

Associated: How you can Use Loops in JavaScript

Here is the construction of a for loop in a shell script:

for VARIABLE in 1 2 3 4 5 .. N
do
command1
command2
commandN
carried out

You possibly can outline the variety of iterations within the first line. This fashion, you may point out the beginning worth and the ending worth.

The variety of iterations is decided by the values you specify, whereas the code following the do assertion is the ensuing loop worth.

Creating and Operating for Loops in Linux Bash

Open the Linux terminal to begin writing code.

MAKEUSEOF VIDEO OF THE DAY

A textual content editor is used to retailer the shell script, which prints the specified outcomes when executed. For illustration functions, the instructions on this information are written within the Nano textual content editor.

Kind nano within the terminal command line to open the textual content editor, adopted by the shell script title.

nano ForLoops.sh

You possibly can change the title of the shell script to no matter you want. The extension is sh, since you may be storing a shell script.

On this part, the next codes will show how one can print integer values in a different way. To make use of a for loop in a shell script to print integers, you possibly can attempt a few of these code examples.

1. Loop Code to Print a Set of Numbers

As soon as the editor opens, it is time to write the code.

#!/usr/bin/bash
for i in 1 2 3
do
echo "Present # $i"
carried out

Linux terminal interface

Output:


Linux terminal interface

The place:

  • i = variable title to retailer the iterated values
  • 1 2 3 = variety of occasions the for loop in shell script iterates
  • do = command to carry out a sure set of actions
  • echo = print the outcomes outlined alongside
  • carried out = finish of the loop

Save the code within the textual content editor by urgent Ctrl + X. Save and exit the script.

Associated: How you can Use For, Whereas, and Do Whereas Loops in Java With Examples

Earlier than executing the code, it’s important to change the shell script’s permissions.

Enter chmod +x adopted by your shell script file title:

chmod +x Forloops.sh

As soon as the permissions are granted, run the for loop in your shell script by typing within the following:

./Forloops.sh

The output will print within the terminal window.

2. Alternate Method to Print a Set of Numbers

There are alternate methods to outline a for loop in a shell script. It’s also possible to specify the beginning and ending worth of the loop’s iterations utilizing curly brackets.

Here is the code construction:

for i in {1..3} # a for loop defines a variable and what number of iterations you need to make by way of a loop
do
echo "Present # $i: Instance 2"
carried out

Linux terminal interface

The loop will run thrice, and the values will probably be printed within the following method:


Linux terminal interface

3. Loop Code Utilizing Step Values

You possibly can outline the step values in your loop if you wish to transfer nonsequentially by way of the iterations. Relying on the worth specified, the output may have a set hole.


For instance:

for i in {1..10..2}
do
echo "Quantity = $i"
carried out

The place:

  • i = variable to retailer the iterations
  • 1..10 = variety of iterations to run the loop
  • 2 = step worth
  • do = command to print the output
  • echo = print command
  • carried out = exit command for the loop

Linux terminal interface

Output:


Linux Terminal interface

The output has a distinction of two, which was specified within the step assertion.

For loops in shell scripting is not restricted to only integers. In Bash, you should use a for loop to successfully iterate by way of characters and string values.

1. Looping By Strings

Here is a fundamental instance of how one can loop by way of some string values (outlined within the for assertion):

for title in John Jack Mary
do
echo "My title is $title"
carried out

The place:

  • title = variable to retailer the string values
  • do = command to print the output
  • echo = print command
  • carried out = exit command for the loop


Linux terminal interface

Output:


Linux terminal interface

This for loop will iterate thrice, as there are solely three string values specified within the for assertion.

2. Looping By Strings With Situations

What if you wish to move some logical circumstances to terminate the loop mid-way? For this objective, you should use logical statements such because the IF assertion. The IF assertion controls how the loop will work and what output will print in consequence.

for aspect in Hydrogen Helium Lithium Beryllium; do

if [[ "$element" == 'Lithium' ]]; then

break

fi

echo "Ingredient: $aspect"

carried out

echo 'All Accomplished!'


Using-conditional-statements-in-Linux-Bash-1

Associated: How you can Use Loops With Lists in PythonAs quickly because the aspect’s worth equals Lithium, the loop terminates, and the output prints. The loop runs till the situation is now not met.

Since Lithium is third within the checklist of values, the loop will run for 2 iterations earlier than it prints the ultimate output All Accomplished!.


Linux terminal interface

Operating Loops in Linux Bash

Loops are an important a part of the Linux shell construction, which may tremendously improve the perform of Linux scripts.

If it’s important to print repetitive outputs, there’s nothing higher than loops inside Bash scripts. As we talked about earlier, loops can be found in practically each programming language, and Python is not any exception. Lower out repetition and reside by the DRY (Do not Repeat Your self) code.


HHow to Use For Loop in Python - Featured
How you can Use For Loops in Python

For Loops are an important programming talent. Learn to use them in Python right here.

Learn Subsequent


About The Creator

Leave a Comment

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