diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/bash/shell-arithmetic.html | |
new repository
Diffstat (limited to 'devdocs/bash/shell-arithmetic.html')
| -rw-r--r-- | devdocs/bash/shell-arithmetic.html | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/devdocs/bash/shell-arithmetic.html b/devdocs/bash/shell-arithmetic.html new file mode 100644 index 00000000..fb62b942 --- /dev/null +++ b/devdocs/bash/shell-arithmetic.html @@ -0,0 +1,23 @@ +<h1 class="section">Shell Arithmetic</h1> <p>The shell allows arithmetic expressions to be evaluated, as one of the shell expansions or by using the <code>((</code> compound command, the <code>let</code> builtin, or the <samp>-i</samp> option to the <code>declare</code> builtin. </p> <p>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, associativity, and values are the same as in the C language. The following list of operators is grouped into levels of equal-precedence operators. The levels are listed in order of decreasing precedence. </p> <dl compact> <dt><span><code><var>id</var>++ <var>id</var>--</code></span></dt> <dd> +<p>variable post-increment and post-decrement </p> </dd> <dt><span><code>++<var>id</var> --<var>id</var></code></span></dt> <dd> +<p>variable pre-increment and pre-decrement </p> </dd> <dt><span><code>- +</code></span></dt> <dd> +<p>unary minus and plus </p> </dd> <dt><span><code>! ~</code></span></dt> <dd> +<p>logical and bitwise negation </p> </dd> <dt><span><code>**</code></span></dt> <dd> +<p>exponentiation </p> </dd> <dt><span><code>* / %</code></span></dt> <dd> +<p>multiplication, division, remainder </p> </dd> <dt><span><code>+ -</code></span></dt> <dd> +<p>addition, subtraction </p> </dd> <dt><span><code><< >></code></span></dt> <dd> +<p>left and right bitwise shifts </p> </dd> <dt><span><code><= >= < ></code></span></dt> <dd> +<p>comparison </p> </dd> <dt><span><code>== !=</code></span></dt> <dd> +<p>equality and inequality </p> </dd> <dt><span><code>&</code></span></dt> <dd> +<p>bitwise AND </p> </dd> <dt><span><code>^</code></span></dt> <dd> +<p>bitwise exclusive OR </p> </dd> <dt><span><code>|</code></span></dt> <dd> +<p>bitwise OR </p> </dd> <dt><span><code>&&</code></span></dt> <dd> +<p>logical AND </p> </dd> <dt><span><code>||</code></span></dt> <dd> +<p>logical OR </p> </dd> <dt><span><code>expr ? expr : expr</code></span></dt> <dd> +<p>conditional operator </p> </dd> <dt><span><code>= *= /= %= += -= <<= >>= &= ^= |=</code></span></dt> <dd> +<p>assignment </p> </dd> <dt><span><code>expr1 , expr2</code></span></dt> <dd><p>comma </p></dd> </dl> <p>Shell variables are allowed as operands; parameter expansion is performed before the expression is evaluated. Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax. A shell variable that is null or unset evaluates to 0 when referenced by name without using the parameter expansion syntax. The value of a variable is evaluated as an arithmetic expression when it is referenced, or when a variable which has been given the <code>integer</code> attribute using ‘<samp>declare -i</samp>’ is assigned a value. A null value evaluates to 0. A shell variable need not have its <code>integer</code> attribute turned on to be used in an expression. </p> <p>Integer constants follow the C language definition, without suffixes or character constants. Constants with a leading 0 are interpreted as octal numbers. A leading ‘<samp>0x</samp>’ or ‘<samp>0X</samp>’ denotes hexadecimal. Otherwise, numbers take the form [<var>base</var><code>#</code>]<var>n</var>, where the optional <var>base</var> is a decimal number between 2 and 64 representing the arithmetic base, and <var>n</var> is a number in that base. If <var>base</var><code>#</code> is omitted, then base 10 is used. When specifying <var>n</var>, if a non-digit is required, the digits greater than 9 are represented by the lowercase letters, the uppercase letters, ‘<samp>@</samp>’, and ‘<samp>_</samp>’, in that order. If <var>base</var> is less than or equal to 36, lowercase and uppercase letters may be used interchangeably to represent numbers between 10 and 35. </p> <p>Operators are evaluated in order of precedence. Sub-expressions in parentheses are evaluated first and may override the precedence rules above. </p><div class="_attribution"> + <p class="_attribution-p"> + Copyright © 2000, 2001, 2002, 2007, 2008 Free Software Foundation, Inc.<br>Licensed under the GNU Free Documentation License.<br> + <a href="https://www.gnu.org/software/bash/manual/html_node/Shell-Arithmetic.html" class="_attribution-link">https://www.gnu.org/software/bash/manual/html_node/Shell-Arithmetic.html</a> + </p> +</div> |
