Gice

Technology and General Blog

In any programming or scripting language, the loop is a quintessential characteristic. Loops are frequently to complete a repetitive activity until finally a certain affliction is fulfilled. Bash is a impressive scripting language that supports all the major functions of a scripting language (including loops).

This information demonstrates one particular-line for loops in Bash.

Bash for loop

The bash attributes numerous loop sorts – for, though, and till. Every single variety of loop will come with a distinct framework. Nonetheless, the fundamentals keep on being the same. For newcomers, this tutorial points out in-depth about various bash loops and how to apply them.

As the title of this guide suggests, our concentrate will be on the loop. Even though for loop typically demands various strains, we can stand for it in a solitary line if the loop is straightforward ample. This approach, having said that, needs an knowledge of the fundamentals of bash for a loop.

To operate our bash codes, we have to have a shell script. I previously have a dummy script to run our codes.

For loop structure

This is the basic construction of the bash for loop.

for  in [list]
    do
       
finished

Here’s a rapid for loop instance applying this structure.

for i in 1..5
do
    echo “amount: $i
completed

Bash also supports C-design and style for loop. If you have programming track record in C, then C-type for loop will be quick to have an understanding of.

Let us set the C-type for loop in motion.

for ((i = 1 i <= 5 i++)) do
    echo “number: $i
done

For loop can also work with files. In the following example, the loop will search all the partitions under the disk “/dev/sda” and print all of it.

for i in /dev/sda* do
    echo $i
done

One line for loop

With the basics covered, we can now compress for loops into a single line. Basically, we’ll eliminate the newlines from the entire for loop code. We can also run these loops directly from the command line.

Let’s compress the first example. If we eliminate all the new lines, the code will look like this.

$ for i in 1..5 do echo “number: $i done

As you can see, all the new lines are removed. Instead, those newlines are replaced with semicolons ().

We can do the same with C-style for loops.

$ for ((i = 1 i <= 5 i++)) do echo “number: $i done

Have a look at the following example. All the configuration files inside “/etc.” will be copied as a backup to the “~/backup” directory.

$ for i in /etc/*.conf do cp $i /home/viktor/backup done

For loop with conditionals

In many cases, a loop will contain conditionals to make decisions at various points of the repetition.

Here, the following for loop will print all the even numbers within a fixed range.

for i in 1..10 do
    if [ $((i%2)) -eq 0 ] then
        echo $i even”
    fi
done

It’s possible to express this entire loop into a single line. Just like before, replace all the newline with semicolons ().

$ for i in 1..10 do if [ $((i%2)) -eq 0 ] then echo $i even” fi done

It’s recommended to write down the loop with proper spacing first. Once the loop is confirmed to work properly, we can safely compress it into a single line.

Miscellaneous examples

Here’s a handful of one line for loops for reference.

$ for i in 1 2 3 4 5 do echo “number: $i done

$ for i in cpu motherboard ram psu gpu do echo “computer part: $i done

The next example will be of an infinite loop.

$ for (( )) do echo “to infinity!” done

Final thought

This guide demonstrates various effective one-line for loop examples. It’s very easy to transform a normal for loop into one line. Hopefully, after practicing these examples, readers will have a good idea of using bash for loop in one line.

Happy computing!

Leave a Reply

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