craphunter
craphunter

Reputation: 830

arithmetic expression: expecting EOF: "008 +1"

My script ./make_shift_ln_o_dummy.sh:

for i in  `seq -w 0 272`
do
y=0
x=1
echo $i
y=$(($i +$x))
echo $y
done

My output with error message: arithmetic expression: expecting EOF: "008 +1"

000
1
001
2
002
3
003
4
004
5
005
6
006
7
007
8
008
./make_shift_ln_o_dummy.sh: 25: arithmetic expression: expecting EOF: "008 +1"

Why does it happen? What do I do wrong? How should I change it to the output of 272?

Upvotes: 1

Views: 18420

Answers (3)

glenn jackman
glenn jackman

Reputation: 246847

008 is an octal number. You can specify you want to use a base-10 number in your arithmetic expression:

y=$((10#$i +$x))

http://www.gnu.org/software/bash/manual/bashref.html#Shell-Arithmetic

Upvotes: 6

msw
msw

Reputation: 43497

Why does it happen?

The bash expression evaluator sees the leading 0 and assumes that an octal constant is going to follow but 8 is not a valid octal digit.

Version 4.2 of bash gives a more helpful diagnostic:

$ echo $((007 + 1))
8
$ echo $((008 + 1))
bash: 008: value too great for base (error token is "008")

The answer from anubhava above gave the "how to fix" which is why I upvoted it.

Upvotes: 5

anubhava
anubhava

Reputation: 785246

No need to use seq here. You can use bash arithmetic features like this:

for ((i=0; i<272; i++))
do
   y=0
   x=1
   printf "%03d\n" $i
   y=$(($i + $x))
   printf "%03d\n" $y
done

Upvotes: 2

Related Questions