Thursday, May 2, 2024
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 37183  / 1 Year ago, thu, january 19, 2023, 3:13:11

I am new to shell scripting so I am curious about using While Loops in Shell Script form.



I know how to declare a variable and use a while loop in java



int num = 0;
while(num <= 10)
{
System.out.println(num)
num = num + 1
}


But I don't know how to declare variables and use them in a while loop in a Shell Script.


More From » command-line

 Answers
2

You'll be much happier using a for loop instead (according to your example)



for i in {0..10}; do
echo "$i"
done


If you must use a while loop, consider the following



i=0
while [[ $i -le 10 ]]; do
echo "$i"
i=$((i+=1))
done

[#28703] Saturday, January 21, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
housecoarmer

Total Points: 434
Total Questions: 116
Total Answers: 120

Location: Albania
Member since Sun, Nov 22, 2020
4 Years ago
;