James PK's Technical Journal


[ Site Home | Journal Home ]


Tue, 18 Feb 2014

Bash - Arithmetic Related Error

I used a date calculation in a script, that failed:

jamespk@hal:~$ let day_of_year=$(date "+%j")
bash: let: day_of_year=049: value too great for base (error token is "049")

It turns out bash is treating the number as octal because of the leading zero.

A solution is to specify the base-10 prefix
jamespk@hal:~$ let day_of_year=10#$(date "+%j")
jamespk@hal:~$ echo $day_of_year
49

posted at: 00:00 | path: /shell | permanent link to this entry


Made with Pyblosxom