1 SC3052
Vidar Holen edited this page 2022-11-02 21:16:26 -07:00

In POSIX sh, arithmetic base conversion is undefined

Problematic code:

#!/bin/sh
echo "Hex $1 is decimal $((16#$1))"

Correct code:

#!/bin/sh
echo "Hex $1 is decimal $(echo "ibase=16; $1" | bc)"

Rationale:

Your script uses a shell feature not supported by the shebang. Either rewrite the script to be portable, or change the shebang to explicitly require a shell like Bash.

In this case, using radix specifiers in arithmetic contexts, such as $((16#DEADBEEF)) is not portable. bc ibase/obase can be used instead.

Exceptions:

None

  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!