Linux Reader
Access files and folders on Ext, UFS, HFS, ReiserFS, or APFS file systems from Windows

Pew Pew madafakas!

Access files and folders on Ext, UFS, HFS, ReiserFS, or APFS file systems from Windows
Last updated: Jul 29, 2021

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:

The syntax for loop

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
Using for in statement
  1. 2. Using for ((exp1, exp2, exp3)) statement
Using for ((exp1, exp2, exp3)) statement

The result will be:

The result of using for ((exp1, exp2, exp3)) statement

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:

The first option of using syntax options

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:

To say “Hello” 3 times

The second option:

The second option of using syntax options

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

It looks like this in the example:

Using i ++

Another example for counting only even numbers:

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:

The command to make a backup copy of PDF files

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

To change the extension of some files

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!

Related articles

FREE DOWNLOADVer 4.18, WinUpgrade to PROFrom $29.95

Please rate this article.
52 reviews