Bash for loop range

In this tutorial, How do I use bash for loop range step N? The seq method is simple. Bash script the essential for DevOps Roles.

Bash function for loop range

f_step_n() {
for i in $(seq 2 3 10)
do 
  RESULT+=$i;
done
echo $RESULT;
}

For example

  • Numbers: ‘123456789’
  • Position: 2
  • Step: 4
  • Result: 37

My Bash for loop range step N

#!/bin/bash
_NUMBERS='123456789'
#Length of NUMBERS
_LENG=${#_NUMBERS}
#Substring (position, length)
_POSIT=${_NUMBERS:$1:1}
_NUMBER=$2
f_step_n() {
local P=$1;
local N=$2;
local L=$3;
for i in $(seq $P $N $L)
do 
  RESULT+=$i;
done
echo $RESULT;
}
echo -e "Numbers: $_NUMBERS\t Length: $_LENG\t Position: $_POSIT\t Step: $_NUMBER";
f_step_n ${_POSIT} ${_NUMBER} ${_LENG}

The screen output terminal:

Bash for loop range

Conclusion

Thought the article, you can use Bash for loop range as above. I hope will this your helpful. More details refer to Bash script.

,

About HuuPV

My name is Huu. I love technology and especially Devops Skill such as Docker, vagrant, git so forth. I likes open-sources. so I created DevopsRoles.com site to share the knowledge that I have learned. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.