Octals in UNIX shell scripting

I have just discovered that if you have a number with a leading zero, such as 08, it is assumed to be an octal number, that is, one of base 8. The upshot of this is that you get errors when you have numbers like 08 and 09 in your arithmetical expressions; they are illegal in octal: 08 should be 10 and 09 should be 11. Og course, as luck would have it, you get exactly these expressions when date/time processing. Luckily you can forced things to be base 10 by having something like 10#08 or, when extracting the minute from a date-time value, 10#$(date +%M). Strange as it might appear, this behaviour is all by design. It is dictated in the POSIX standard that governs UNIX. That said, I’d rather it if 08 was interpreted as an 8 and 09 as a 9 rather than triggering the errors that we see but that could have been seen as muddying the simplicity of the standard.

Leave a Reply