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:
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.