Bash – How to convert a string into a number interpreted in certain base in bash script

arithmeticbashshell-script

I'm simply trying to convert a string $RECORD_HDR to a number $RECORD_SIZE, where

echo $RECORD_HDR gives 003D
and
echo $RECORD_SIZE should give 61

Best Answer

From bash manual:

Constants with a leading 0 are interpreted as octal numbers. A leading 0x or 0X denotes hexadecimal.

Thus:

$ RECORD_HDR="003D"
$ RECORD_SIZE=$((0x$RECORD_HDR))
$ echo "$RECORD_SIZE"
61
Related Question