Bash: Variable type confusion

Joel Hammer Joel
Mon May 17 11:39:07 PDT 2004


I have always thought that bash does the right thing when it evaluates
variables.
Until today.
This bit of script works as expected:
k=0
for i in `dir -1 *jpg`
do
k=$((k+1))
echo $k
done

It prints out the numbers from 1 to whatever the number of jpg's in the
directory.

However, this fails:
k=0
for i in `dir -1 *jpg`
do
k=$((k+1))
[ "$k" -eq 10 ] && k=x   <---The problem.
echo $k
done

Here, it prints out 1 to 9 and then every tenth item is an x, followed by 1
to 9 again.

What I am trying to do is put leading zeroes on my numbers less that 10.
Attempting that, with [ "$k" -lt 10 ] && k=0"$k" give a base error at 8.

Any insight appreciated.

Joel




More information about the Linux-users mailing list