سؤال

The following code prints 4 as expected:

let x=21
let x=$x-1
echo $x

But the following prints 16:

let x=000021
let x=$x-1
echo $x

Could someone explain the difference?

هل كانت مفيدة؟

المحلول

00021 is an octal constant. The output 16 is the correct decimal result. To get the octal value use

printf "%o\n" $x

Remove the leading zeros if the number is decimal.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top