10
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

$(( ))を使っていて”value too great for base”エラーが出る場合のチェックポイント

Last updated at Posted at 2013-07-16
test.sh
for i in `seq -w 1 10`;
  do
    echo $(($i+10));
  done

-wで足りない桁をゼロ埋めすると

01 02 03 04 05 06 07 08 09 10

$iの値が回ることになるが、$(( ))を使って計算する場合は先頭が0で始まる文字が8進数とみなされるので08の時点でエラーとなりスクリプトが終了してしまいます。

$ bash test.sh
11
12
13
14
15
16
17
test.sh: line 4: 08: value too great for base (error token is "08")

exprのほうがいいかも?

10進数の計算が主の場合はexprを使っておいたほうがいいかも。

for i in `seq -w 1 10`;
  do
    echo `expr $i + 10`;
  done
$ bash test.sh
11
12
13
14
15
16
17
18
19
20

$(( ))で便利なところ

16進数と10進数を足してみる

echo $(( 0x10 + 10 ))

26

exprだと

echo `expr 0x10 + 10 `

expr: not a decimal number: '0x10'

結論?

頭に0がつくと8進数として解釈される事があるよと頭の隅に入れておけばなんかの時に助かるかも

10
5
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?