Home > Back-end >  How can i make my arguments my starting and ending number in my loop
How can i make my arguments my starting and ending number in my loop

Time:04-28

my script

how can i make the first argument become the first number in the loop and make my second argument become the second number in the loop?

CodePudding user response:

We can use a new variable $counter to avoid changing the value of your variables as follows. Obviously you can do other things with the values than echo them to STDOUT.

#!/bin/bash
bhuser1=2
bhuser2=5
counter=$bhuser1
while [ $counter -le $bhuser2 ]
do
  echo $counter
  ((counter  ))
done

output

2
3
4
5

[Execution complete with exit code 0]

I have found the following tutorial helpful: https://ryanstutorials.net/bash-scripting-tutorial/bash-loops.php

CodePudding user response:

The range expression { .. } unfortunately does not accept variables, because range expansion happens before parameter expansion. You have to implement a countint loop explicitly, something like.

for ((i=bhuser1; i<=bhuser2; i =1))
do
  ....
done
  •  Tags:  
  • bash
  • Related