old-www/LDP/abs/html/numerical-constants.html

273 lines
4.4 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML
><HEAD
><TITLE
>Numerical Constants</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Advanced Bash-Scripting Guide"
HREF="index.html"><LINK
REL="UP"
TITLE="Operations and Related Topics"
HREF="operations.html"><LINK
REL="PREVIOUS"
TITLE="Operators"
HREF="ops.html"><LINK
REL="NEXT"
TITLE="The Double-Parentheses Construct"
HREF="dblparens.html"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Advanced Bash-Scripting Guide: </TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="ops.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 8. Operations and Related Topics</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="dblparens.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="NUMERICAL-CONSTANTS"
></A
>8.2. Numerical Constants</H1
><P
><A
NAME="NUMCONSTANTS"
></A
>A shell script interprets a number
as decimal (base 10), unless that number has a
special prefix or notation. A number preceded by a
<TT
CLASS="REPLACEABLE"
><I
>0</I
></TT
> is <TT
CLASS="REPLACEABLE"
><I
>octal</I
></TT
>
(base 8). A number preceded by <TT
CLASS="REPLACEABLE"
><I
>0x</I
></TT
>
is <TT
CLASS="REPLACEABLE"
><I
>hexadecimal</I
></TT
> (base 16). A number
with an embedded <TT
CLASS="REPLACEABLE"
><I
>#</I
></TT
> evaluates as
<TT
CLASS="REPLACEABLE"
><I
>BASE#NUMBER</I
></TT
> (with range and notational
restrictions).</P
><DIV
CLASS="EXAMPLE"
><A
NAME="NUMBERS"
></A
><P
><B
>Example 8-4. Representation of numerical constants</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="PROGRAMLISTING"
>#!/bin/bash
# numbers.sh: Representation of numbers in different bases.
# Decimal: the default
let "dec = 32"
echo "decimal number = $dec" # 32
# Nothing out of the ordinary here.
# Octal: numbers preceded by '0' (zero)
let "oct = 032"
echo "octal number = $oct" # 26
# Expresses result in decimal.
# --------- ------ -- -------
# Hexadecimal: numbers preceded by '0x' or '0X'
let "hex = 0x32"
echo "hexadecimal number = $hex" # 50
echo $((0x9abc)) # 39612
# ^^ ^^ double-parentheses arithmetic expansion/evaluation
# Expresses result in decimal.
# Other bases: BASE#NUMBER
# BASE between 2 and 64.
# NUMBER must use symbols within the BASE range, see below.
let "bin = 2#111100111001101"
echo "binary number = $bin" # 31181
let "b32 = 32#77"
echo "base-32 number = $b32" # 231
let "b64 = 64#@_"
echo "base-64 number = $b64" # 4031
# This notation only works for a limited range (2 - 64) of ASCII characters.
# 10 digits + 26 lowercase characters + 26 uppercase characters + @ + _
echo
echo $((36#zz)) $((2#10101010)) $((16#AF16)) $((53#1aA))
# 1295 170 44822 3375
# Important note:
# --------------
# Using a digit out of range of the specified base notation
#+ gives an error message.
let "bad_oct = 081"
# (Partial) error message output:
# bad_oct = 081: value too great for base (error token is "081")
# Octal numbers use only digits in the range 0 - 7.
exit $? # Exit value = 1 (error)
# Thanks, Rich Bartell and Stephane Chazelas, for clarification.</PRE
></FONT
></TD
></TR
></TABLE
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="ops.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="dblparens.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Operators</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="operations.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>The Double-Parentheses Construct</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>