Linux Reader 4.5Ext 2/3/4, UFS2, HFS and ReiserFS/4 for Windows
Linux Reader 4.5
Ext 2/3/4, UFS2, HFS and ReiserFS/4 for Windows

How to Use a Bash for Loop in One Line?

Here you will find out:

  • what a bash for loop is
  • different ways to use bash for loop
  • how to use a one-line bash for loop
  • when DiskInternals can help you

Are you ready? Let's read!

What is a bash for loop?

To begin with, loops are a must-have component of any programming language, since they allow us to perform a huge number of different operations that are incredibly difficult to do manually. This article will cover the “for” loop. It sequentially performs the same actions a certain number of times (for example, it will display a message like “Hello” 100 times). The for loop has one distinguishing feature: the presence of an explicit loop variable or a loop counter. The essence of this is that the body of the for loop has data about the sequence of each iteration.

The syntax for this loop is as follows:

Different ways to use bash for loop

There are two options that you can use to interpret this loop. Moreover, the result will be the same in both cases, so choose what is most suitable for you and proceed.

  1. 1. Using for ... in statement
  1. 2. Using for ((exp1, exp2, exp3)) statement

The result will be:

Why you can use a bash for loop in one line

If you use bash on the command line, this will not guarantee that for loops are inserted into the script. In order to prevent this, it is easiest to use loops directly on the command line and then there will be no problems.

How to use a one-line bash for loop

Use one of the syntax options and see how it will look clearly with examples.

The first option:

Remember that n = a number with a lower value, and m = a number with a larger value. Therefore, if you want to say “Hello” 3 times, then it will look like this:

The second option:

Note: i ++ is a counter that increments by one.

It looks like this in the example:

Another example for counting only even numbers:

Using the for loop on the command line is also very convenient for working with files.

For example, you need a backup copy of some PDF files, then the command will look like this:

Or suppose you need to change the extension of some files from .txt to .bak:

As you can see here, much can be done depending on your preferences and requirements.

DiskInternals Linux Reader to open Linux files

If you are Windows owner and use a virtual machine or a dual-boot installation, you can not do without DiskInternals Linux Reader, as this application quickly and free of charge allows you to get files from Linux to Windows. The application has the ability to read files from various device types: hard drive, SSD, flash drive, memory card, and others. Linux Reader allows you to open the files and re-save it on Windows. Using this application, you can also create a disk image; it never hurts!

FREE DOWNLOADVer 4.5, Win Upgrade to PROFrom $29.95

0.6540310382843