Bash for Loop With Range {#..#}

The for loop is the programming functionality that is used for executing multiple sets of commands in the bash script. In the bash script, the for loop has the feature to define the set of ranges for numbers or alphabets. These numbers or alphabets can be used to perform various tasks in the script. 

This article will enlighten the working of the bash for loop with range {#..#} through the command line.

Bash for Loop With Range {#..#}

The syntax for using the for loop with range is given below:

Syntax:

#!/bin/bashfor var in {RANGE}
do
  Perform_Operations "$var"
done

Use the for loop, a variable, and specify the range. Then, use the do statement to perform operations on that variable.

Example 1: for Loop Range With Range

The following Bash script presents the basic use of for loop with range: 

#!/bin/bash
for d in {1..10}
do
  echo "The number is: $d"
done

The script is described as: 

  • The range of the for loop is set from 1 to 10 inside the curly brackets.
  • The “d” variable will iterate each number of the range.
  • Until the range is true, the “do” will print the statements inside it. 

After saving the script, execute the bash script file for displaying the results:

$ bash ./script.sh

The number of ranges defined in the script has been printed.

Example 2: for Loop Range With Increment 

The user can also define the increment in the range with additional double dots. In the below example, the number of ranges is set to 2 to 10 with an increment of 2. This means the number will be printed with the increment of 2:

#!/bin/bash
for d in {2..10..2}
do
  echo "The number is: $d"
done

The script is described as: 

  • The range of the for loop is set from 2 to 10 in the first double dots and with the increment of 2 in second.
  • The “d” variable will iterate each number of the range.
  • The “do” statement will display the “echo” statement.

Save the above script and execute the script file in the terminal:

$ bash ./script.sh

The results have been printed with the increment of 2.

Example 3: for Loop Range With seq Utility

The seq utility can also be used for defining the range in for loop using the below syntax:

$ seq [start] [increment] [Last]
  • Write the “seq” command.
  • “Start ” is representing the starting range.
  • “Increment” portion applies increment on the range. 
  • “Last” is the ending number of the range.

The below example will print numbers from 5 to 30 with an increment of 3:

#!/bin/bash
for d in $(seq 5 3 30)
do
  echo "The number is: $d"
done

Execute the above script file for getting the results:

$ bash ./script.sh

The number has been printed with the increment of 3.

Example 4: for Loop Range With Bash Variable

To use the bash variable for defining the for loop range c-style for loop is used as described in the below syntax:

Syntax:

#!/bin/bashfor (([INITIALIZER]; [CONDITION]; [INCREMENT] ))
do
  echo = [VARIABLE]

done
  • for loop with “initializer”, “condition” and “increment” variables.
  • “do” statement, to iterate output of the variables 

Check the below example in which starting number is set to 3 and the ending number is 9:

#!/bin/bash
#Bash Variables
start=3
end=9
#c-style for Loop
for (( d=$start; d<=$end; d++ ))
do
  echo "The Number is: $d"

done

The script is described as: 

  • Two bash variables start =3 and end = “9” 
  • “d” variable to initialize the for loop components
  • The “do” statement that print the output of the “d” variable.

Save and execute the file through “bash”:

$ bash ./script.sh

The number has been printed using bash variables.

Example 5: for Loop Range With User Input

To take bash variable from the user input, the user can use the echo statement and “read” property of the bash script:

#!/bin/bash
#Bash Variables
echo "Enter the Starting Number For the Loop:"
read start
echo "Enter the Ending Number For the Loop:"
read end

#c-style for Loop

for (( d=$start; d<=$end; d++ ))
do
  echo "The Number is: $d"

done

The script is described as: 

  • Two echo statement to take input from the user and storing it in the “start” and “end” variables using the “read” property of bash.
  • Initializing “d” with inputs variable in for loop.
  • The “do” statement will print the output of the “d” variable.

Save and execute the above script:

$ bash ./script.sh

The for loop result has been generated according to the user input.

Conclusion

To define the set of ranges in the for loop of the bash script, the “{#..#}” syntax is followed by the starting and the ending range. Apart from this, the range of the for loop can also be defined using the seq utility, bash variables, or from the user input. This write-up has illustrated the bash-for-loop examples for setting the range.