tarang ranpara
tarang ranpara

Reputation: 149

why this simple while loop not working in bash script?

#! /bin/bash
read -p "enter i:" I
while [ $I -lt 4 ]
do
echo $I
I=$[$I+1]
done

Upvotes: 0

Views: 340

Answers (1)

Gilles Quénot
Gilles Quénot

Reputation: 185831

In modern :

read -p 'enter a positive integer < 4: >>> ' int

while ((int < 4 )); do
        echo "$int"
        ((int++))
done

((...))

is an arithmetic command, which returns an exit status of 0 if the expression is nonzero, or 1 if the expression is zero. Also used as a synonym for "let", if side effects (assignments) are needed. See http://mywiki.wooledge.org/ArithmeticExpression

Upvotes: 2

Related Questions