| Advanced Bash-Scripting Guide: A complete guide to shell scripting, using Bash | ||
|---|---|---|
| Prev | Chapter 8. Operations and Related Topics | Next |
A shell script interprets a number as decimal (base 10), unless that number has a special prefix or notation. A number preceded by a 0 is octal (base 8). A number preceded by 0x is hexadecimal (base 16). A number with an embedded # is evaluated as BASE#NUMBER (this option is of limited usefulness because of range restrictions).
Example 8-3. Representation of numerical constants:
#!/bin/bash
# numbers.sh: Representation of numbers.
# Decimal
let "d = 32"
echo "d = $d"
# Nothing out of the ordinary here.
# Octal: numbers preceded by '0' (zero)
let "o = 071"
echo "o = $o"
# Expresses result in decimal.
# Hexadecimal: numbers preceded by '0x' or '0X'
let "h = 0x7a"
echo "h = $h"
# Expresses result in decimal.
# Other bases: BASE#NUMBER
# BASE between 2 and 36.
let "b = 32#77"
echo "b = $b"
#
# This notation only works for a limited range (2 - 36)
# ... 10 digits + 26 alpha characters = 36.
let "c = 2#47" # Out of range error:
# numbers.sh: let: c = 2#47: value too great for base (error token is "2#47")
echo "c = $c"
echo
echo $((36#zz)) $((2#10101010)) $((16#AF16))
exit 0
# Thanks, S.C., for clarification. |