MOHAMED
MOHAMED

Reputation: 43558

How to do treat octal numbers as decimal when doing arithmetics under busybox' ash

I have decimal numbers stored in strings.

The numbers which are < 100 are stored in this way "045" or "005".

When using these number strings in arithmetic operations like let A="045"+"009" these numbers are treated as octal numbers like indicated in the man page.

To treat them as decimal I added 10# at the beginning of the number strings like that

let A="10#045"+"10#123"

but this solution causes an error -ash: let: arithmetic syntax error in my bash from BusyBox (Installed on OpenWRT)

Is there another solution for my busybox shell?

Note: The operation should evaluated with let because I need theses numbers in other kind of operations like bitwise operation.

Upvotes: 2

Views: 955

Answers (2)

Adrian Fr&#252;hwirth
Adrian Fr&#252;hwirth

Reputation: 45656

busybox does not have bash, its shell is ash.

You can either strip the leading zeros off your variables, e.g.:

while [ "${n:0:1}" = "0" ]; do n="${n#?}"; done

or use expr:

$ echo $(expr 045 + 045)
90

Upvotes: 4

choroba
choroba

Reputation: 242103

You can remove the zeros before doing the arithmetics:

n=005
shopt -s extglob
n1=${n##+(0)}
echo $n1

Output:

5

Upvotes: 0

Related Questions