|
发表于 2004-6-7 14:17:44
|
显示全部楼层
更详尽的解释:- Compound Commands
- ...
- [color=red]((expression))[/color]
- The expression is evaluated according to the rules described
- below under [color=red]ARITHMETIC EVALUATION[/color]. If the value of the expres-
- sion is non-zero, the return status is 0; otherwise the return
- status is 1. This is exactly equivalent to let "expression".
- ...
- Arithmetic Expansion
- Arithmetic expansion allows the evaluation of an arithmetic expression
- and the substitution of the result. The format for arithmetic expan-
- sion is:
- [color=red]$((expression))[/color]
- The expression is treated as if it were within double quotes, but a
- double quote inside the parentheses is not treated specially. All
- tokens in the expression undergo parameter expansion, string expansion,
- command substitution, and quote removal. Arithmetic substitutions may
- be nested.
- The evaluation is performed according to the rules listed below under
- [color=red]ARITHMETIC EVALUATION[/color]. If expression is invalid, bash prints a message
- indicating failure and no substitution occurs.
- [color=red]ARITHMETIC EVALUATION[/color]
- The shell allows arithmetic expressions to be evaluated, under certain
- circumstances (see the let builtin command and Arithmetic Expansion).
- Evaluation is done in fixed-width integers with no check for overflow,
- though division by 0 is trapped and flagged as an error. The operators
- and their precedence and associativity are the same as in the C lan-
- guage. The following list of operators is grouped into levels of
- equal-precedence operators. The levels are listed in order of decreas-
- ing precedence.
- id++ id--
- variable post-increment and post-decrement
- ++id --id
- variable pre-increment and pre-decrement
- - + unary minus and plus
- ! ~ logical and bitwise negation
- ** exponentiation
- * / % multiplication, division, remainder
- + - addition, subtraction
- << >> left and right bitwise shifts
- <= >= < >
- comparison
- == != equality and inequality
- & bitwise AND
- ^ bitwise exclusive OR
- | bitwise OR
- && logical AND
- || logical OR
- expr?expr:expr
- conditional evaluation
- = *= /= %= += -= <<= >>= &= ^= |=
- assignment
- expr1 , expr2
- comma
- Shell variables are allowed as operands; parameter expansion is per-
- formed before the expression is evaluated. Within an expression, shell
- variables may also be referenced by name without using the parameter
- expansion syntax. The value of a variable is evaluated as an arith-
- metic expression when it is referenced. A shell variable need not have
- its integer attribute turned on to be used in an expression.
- Constants with a leading 0 are interpreted as octal numbers. A leading
- 0x or 0X denotes hexadecimal. Otherwise, numbers take the form
- [base#]n, where base is a decimal number between 2 and 64 representing
- the arithmetic base, and n is a number in that base. If base# is omit-
- ted, then base 10 is used. The digits greater than 9 are represented
- by the lowercase letters, the uppercase letters, @, and _, in that
- order. If base is less than or equal to 36, lowercase and uppercase
- letters may be used interchangably to represent numbers between 10 and
- 35.
- Operators are evaluated in order of precedence. Sub-expressions in
- parentheses are evaluated first and may override the precedence rules
- above.
复制代码 |
|