How to Bash Ranges

  1. Use the for Loop to Get Range in Bash
  2. Use the for Loop With Three-Expression to Get Range in Bash
  3. Use the while Loop to Get Range in Bash
  4. Use eval to Get Range in Bash
  5. Use seq to Get Range in Bash
  6. FAQ
How to Bash Ranges

When it comes to scripting in Bash, one of the most fundamental tasks is iterating through a range of integers. Whether you’re automating a system task, processing files, or managing system configurations, knowing how to effectively loop through numbers can save you time and effort. In this article, we will explore two common methods for achieving this: using a for loop and a while loop.

Understanding these techniques not only enhances your scripting skills but also opens the door to more complex automation tasks. So, whether you’re a seasoned developer or a newcomer to Bash scripting, this guide will provide you with the insights you need to master integer iteration in Bash.

Use the for Loop to Get Range in Bash

The bash script below uses a for loop with brace expansion to loop through a range of numbers. The numbers inside the braces are incremented sequentially.

#!/bin/bash

printf "Print Numbers from 0 to 5\n"
for x in {0..5}
do
printf "number:$x\n"
done

Output:

Print Numbers from 0 to 5
number:0
number:1
number:2
number:3
number:4
number:5

Use the for Loop With Three-Expression to Get Range in Bash

The script uses a for loop with a three-expression like in C language. Here, expr1 is the initialization, expr2 is the condition, and expr3 is the increment.

In our case, x is initialized to 0, x is tested if it is less than or equal to 5, and lastly, x is incremented by 1.

#!/bin/bash

max=5
printf "Print Numbers from 0 to $max\n"
for ((x=0;x<=max;x++)); do
    printf "number: $x\n"
done

Output:

Print Numbers from 0 to 5
number: 0
number: 1
number: 2
number: 3
number: 4
number: 5

Use the while Loop to Get Range in Bash

This uses a while loop with a binary comparison operator, -le, that is used for arithmetic value comparison.

In our case, the while loop executes as long as x is less than or equal to the variable $max.

#!/bin/bash

x=0
max=5
printf "Print numbers from $x to $max\n"
while [ $x -le $max ]; do
    printf "number: $x\n"
    x=$(($x+1))
done

Output:

Print numbers from 0 to 5
number: 0
number: 1
number: 2
number: 3
number: 4
number: 5

Use eval to Get Range in Bash

eval is a bash command used to execute arguments as shell commands. In the script below, the braces generate numbers from 0 to $max in increments of 1, the for loop iterates over these numbers, and the printf command displays them.

#!/bin/bash

max=4

printf "Print Numbers from 0 to $max\n"
for x in `eval echo {0..$max}`
do
printf "number: $x\n"
done

Output:

Print Numbers from 0 to 4
number: 0
number: 1
number: 2
number: 3
number: 4

Use seq to Get Range in Bash

seq is a bash command used to generate numbers from start to finish in increment steps.

In our case, seq generates numbers from x to $max in increments of 1. The for loop iterates over these numbers, and the printf command is used to display them.

x=0
max=5

printf "Print numbers from $x to $max\n"
for x in $(seq $x $max)
do
printf "number: $x\n"
done

Output:

Print numbers from 0 to 5
number: 0
number: 1
number: 2
number: 3
number: 4
number: 5

FAQ

  1. What is the difference between a for loop and a while loop in Bash?
    A for loop iterates over a predefined set of values, while a while loop continues until a specified condition is no longer true.

  2. Can I use floating-point numbers in Bash loops?
    Bash does not natively support floating-point arithmetic, so you would need to use external tools like bc for such calculations.

  3. How can I iterate over a specific step size in a for loop?
    You can specify a step size in a for loop using the syntax {start..end..step}, for example, {1..10..2} to iterate over odd numbers.

  4. Is it possible to exit a loop prematurely in Bash?
    Yes, you can use the break command to exit a loop early based on a condition.

  5. Can I nest loops in Bash?
    Yes, you can nest loops in Bash. Just ensure that you properly manage the variable scopes to avoid conflicts.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Fumbani Banda avatar Fumbani Banda avatar

Fumbani is a tech enthusiast. He enjoys writing on Linux and Python as well as contributing to open-source projects.

LinkedIn GitHub

Related Article - Bash Loop