Integer expression expected
Your assignment X=$X+1
doesn't perform arithmetic. If $X
is 1, it sets it to the string "1+1"
. Change X=$X+1
to let X=X+1
or let X++
.
As for the use of -lt
rather than <
, that's just part of the syntax of [
(i.e., the test
command). It uses =
and !=
for string equality and inequality -eq
, -ne
, -lt
, -le
, -gt
, and -ge
for numbers. As @Malvolio points out, the use of <
would be inconvenient, since it's the input redirection operator.
(The test
/ [
command that's built into the bash shell does accept <
and >
, but not <=
or >=
, for strings. But the <
or >
character has to be quoted to avoid interpretation as an I/O redirection operator.)
Or consider using the equivalent (( expr ))
construct rather than the let
command. For example, let X++
can be written as ((X++))
. At least bash, ksh, and zsh support this, though sh likely doesn't. I haven't checked the respective documentation, but I presume the shells' developers would want to make them compatible.
I would use
X=`expr $X + 1`
but that's just me. And you cannot say $X < 20 because < is the input-redirect operator.
The sum X=$X+1
should be X=$(expr $X + 1 )
.
You can also use <
for the comparison, but you have to write (("$X" < "20"))
with the double parenthesis instead of [ $X -lt 20 ]
.