591 lines
28 KiB
HTML
591 lines
28 KiB
HTML
<!DOCTYPE HTML>
|
||
<html lang="en" class="sidebar-visible no-js light">
|
||
<head>
|
||
<!-- Book generated using mdBook -->
|
||
<meta charset="UTF-8">
|
||
<title>Arithmetic Evaluation - Zsh Manual</title>
|
||
|
||
|
||
|
||
|
||
<!-- Custom HTML head -->
|
||
|
||
|
||
|
||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
||
<meta name="description" content="">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<meta name="theme-color" content="#ffffff" />
|
||
|
||
|
||
<link rel="icon" href="favicon.svg">
|
||
|
||
|
||
<link rel="shortcut icon" href="favicon.png">
|
||
|
||
<link rel="stylesheet" href="css/variables.css">
|
||
<link rel="stylesheet" href="css/general.css">
|
||
<link rel="stylesheet" href="css/chrome.css">
|
||
|
||
<link rel="stylesheet" href="css/print.css" media="print">
|
||
|
||
|
||
<!-- Fonts -->
|
||
<link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
|
||
|
||
<link rel="stylesheet" href="fonts/fonts.css">
|
||
|
||
|
||
<!-- Highlight.js Stylesheets -->
|
||
<link rel="stylesheet" href="highlight.css">
|
||
<link rel="stylesheet" href="tomorrow-night.css">
|
||
<link rel="stylesheet" href="ayu-highlight.css">
|
||
|
||
<!-- Custom theme stylesheets -->
|
||
|
||
|
||
|
||
</head>
|
||
<body>
|
||
<!-- Provide site root to javascript -->
|
||
<script type="text/javascript">
|
||
var path_to_root = "";
|
||
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
|
||
</script>
|
||
|
||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
||
<script type="text/javascript">
|
||
try {
|
||
var theme = localStorage.getItem('mdbook-theme');
|
||
var sidebar = localStorage.getItem('mdbook-sidebar');
|
||
|
||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
||
}
|
||
|
||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
||
}
|
||
} catch (e) { }
|
||
</script>
|
||
|
||
<!-- Set the theme before any content is loaded, prevents flash -->
|
||
<script type="text/javascript">
|
||
var theme;
|
||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
||
if (theme === null || theme === undefined) { theme = default_theme; }
|
||
var html = document.querySelector('html');
|
||
html.classList.remove('no-js')
|
||
html.classList.remove('light')
|
||
html.classList.add(theme);
|
||
html.classList.add('js');
|
||
</script>
|
||
|
||
<!-- Hide / unhide sidebar before it is displayed -->
|
||
<script type="text/javascript">
|
||
var html = document.querySelector('html');
|
||
var sidebar = 'hidden';
|
||
if (document.body.clientWidth >= 1080) {
|
||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
||
sidebar = sidebar || 'visible';
|
||
}
|
||
html.classList.remove('sidebar-visible');
|
||
html.classList.add("sidebar-" + sidebar);
|
||
</script>
|
||
|
||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
||
<div class="sidebar-scrollbox">
|
||
<ol class="chapter"><li class="chapter-item expanded "><a href="The-Z-Shell-Manual.html"><strong aria-hidden="true">1.</strong> The Z Shell Manual</a></li><li class="chapter-item expanded "><a href="Introduction.html"><strong aria-hidden="true">2.</strong> Introduction</a></li><li class="chapter-item expanded "><a href="Roadmap.html"><strong aria-hidden="true">3.</strong> Roadmap</a></li><li class="chapter-item expanded "><a href="Invocation.html"><strong aria-hidden="true">4.</strong> Invocation</a></li><li class="chapter-item expanded "><a href="Files.html"><strong aria-hidden="true">5.</strong> Files</a></li><li class="chapter-item expanded "><a href="Shell-Grammar.html"><strong aria-hidden="true">6.</strong> Shell Grammar</a></li><li class="chapter-item expanded "><a href="Redirection.html"><strong aria-hidden="true">7.</strong> Redirection</a></li><li class="chapter-item expanded "><a href="Command-Execution.html"><strong aria-hidden="true">8.</strong> Command Execution</a></li><li class="chapter-item expanded "><a href="Functions.html"><strong aria-hidden="true">9.</strong> Functions</a></li><li class="chapter-item expanded "><a href="Jobs-_0026-Signals.html"><strong aria-hidden="true">10.</strong> Jobs & Signals</a></li><li class="chapter-item expanded "><a href="Arithmetic-Evaluation.html" class="active"><strong aria-hidden="true">11.</strong> Arithmetic Evaluation</a></li><li class="chapter-item expanded "><a href="Conditional-Expressions.html"><strong aria-hidden="true">12.</strong> Conditional Expressions</a></li><li class="chapter-item expanded "><a href="Prompt-Expansion.html"><strong aria-hidden="true">13.</strong> Prompt Expansion</a></li><li class="chapter-item expanded "><a href="Expansion.html"><strong aria-hidden="true">14.</strong> Expansion</a></li><li class="chapter-item expanded "><a href="Parameters.html"><strong aria-hidden="true">15.</strong> Parameters</a></li><li class="chapter-item expanded "><a href="Options.html"><strong aria-hidden="true">16.</strong> Options</a></li><li class="chapter-item expanded "><a href="Shell-Builtin-Commands.html"><strong aria-hidden="true">17.</strong> Shell Builtin Commands</a></li><li class="chapter-item expanded "><a href="Zsh-Line-Editor.html"><strong aria-hidden="true">18.</strong> Zsh Line Editor</a></li><li class="chapter-item expanded "><a href="Completion-Widgets.html"><strong aria-hidden="true">19.</strong> Completion Widgets</a></li><li class="chapter-item expanded "><a href="Completion-System.html"><strong aria-hidden="true">20.</strong> Completion System</a></li><li class="chapter-item expanded "><a href="Completion-Using-compctl.html"><strong aria-hidden="true">21.</strong> Completion Using compctl</a></li><li class="chapter-item expanded "><a href="Zsh-Modules.html"><strong aria-hidden="true">22.</strong> Zsh Modules</a></li><li class="chapter-item expanded "><a href="Calendar-Function-System.html"><strong aria-hidden="true">23.</strong> Calendar Function System</a></li><li class="chapter-item expanded "><a href="TCP-Function-System.html"><strong aria-hidden="true">24.</strong> TCP Function System</a></li><li class="chapter-item expanded "><a href="Zftp-Function-System.html"><strong aria-hidden="true">25.</strong> Zftp Function System</a></li><li class="chapter-item expanded "><a href="User-Contributions.html"><strong aria-hidden="true">26.</strong> User Contributions</a></li></ol>
|
||
</div>
|
||
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
|
||
</nav>
|
||
|
||
<div id="page-wrapper" class="page-wrapper">
|
||
|
||
<div class="page">
|
||
|
||
<div id="menu-bar-hover-placeholder"></div>
|
||
<div id="menu-bar" class="menu-bar sticky bordered">
|
||
<div class="left-buttons">
|
||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
||
<i class="fa fa-bars"></i>
|
||
</button>
|
||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
||
<i class="fa fa-paint-brush"></i>
|
||
</button>
|
||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
||
<li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
|
||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
||
</ul>
|
||
|
||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
||
<i class="fa fa-search"></i>
|
||
</button>
|
||
|
||
</div>
|
||
|
||
<h1 class="menu-title">Zsh Manual</h1>
|
||
|
||
<div class="right-buttons">
|
||
|
||
<a href="print.html" title="Print this book" aria-label="Print this book">
|
||
<i id="print-button" class="fa fa-print"></i>
|
||
</a>
|
||
|
||
|
||
|
||
|
||
</div>
|
||
</div>
|
||
|
||
|
||
<div id="search-wrapper" class="hidden">
|
||
<form id="searchbar-outer" class="searchbar-outer">
|
||
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
||
</form>
|
||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
||
<div id="searchresults-header" class="searchresults-header"></div>
|
||
<ul id="searchresults">
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
|
||
|
||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
||
<script type="text/javascript">
|
||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
||
});
|
||
</script>
|
||
|
||
<div id="content" class="content">
|
||
<main>
|
||
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
||
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
||
<p><strong>Table of Contents</strong> <em>generated with <a href="https://github.com/thlorenz/doctoc">DocToc</a></em></p>
|
||
<ul>
|
||
<li><a href="#11-arithmetic-evaluation">11 Arithmetic Evaluation</a></li>
|
||
</ul>
|
||
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
||
<p><span id="Arithmetic-Evaluation"></span>
|
||
<span id="Arithmetic-Evaluation-1"></span></p>
|
||
<h1 id="11-arithmetic-evaluation"><a class="header" href="#11-arithmetic-evaluation">11 Arithmetic Evaluation</a></h1>
|
||
<p><span id="index-arithmetic-evaluation"></span>
|
||
<span id="index-evaluation_002c-arithmetic"></span>
|
||
<span id="index-let_002c-use-of"></span></p>
|
||
<p>The shell can perform integer and floating point arithmetic, either
|
||
using the builtin <code>let</code>, or via a substitution of the form
|
||
<code>$((``...``))</code>. For integers, the shell is usually compiled to use
|
||
8-byte precision where this is available, otherwise precision is 4
|
||
bytes. This can be tested, for example, by giving the command ‘<code>print - $(( 12345678901 ))</code>’; if the number appears unchanged, the precision is
|
||
at least 8 bytes. Floating point arithmetic always uses the ‘double’
|
||
type with whatever corresponding precision is provided by the compiler
|
||
and the library.</p>
|
||
<p>The <code>let</code> builtin command takes arithmetic expressions as arguments;
|
||
each is evaluated separately. Since many of the arithmetic operators, as
|
||
well as spaces, require quoting, an alternative form is provided: for
|
||
any command which begins with a ‘<code>((</code>’, all the characters until a
|
||
matching ‘<code>))</code>’ are treated as a quoted expression and arithmetic
|
||
expansion performed as for an argument of <code>let</code>. More precisely,
|
||
‘<code>((``...``))</code>’ is equivalent to ‘<code>let "``...``"</code>’. The return status
|
||
is 0 if the arithmetic value of the expression is non-zero, 1 if it is
|
||
zero, and 2 if an error occurred.</p>
|
||
<p>For example, the following statement</p>
|
||
<div class="example">
|
||
<pre><code class="language-example">(( val = 2 + 1 ))
|
||
</code></pre>
|
||
</div>
|
||
<p>is equivalent to</p>
|
||
<div class="example">
|
||
<pre><code class="language-example">let "val = 2 + 1"
|
||
</code></pre>
|
||
</div>
|
||
<p>both assigning the value 3 to the shell variable <code>val</code> and returning a
|
||
zero status.</p>
|
||
<p><span id="index-arithmetic-base"></span>
|
||
<span id="index-bases_002c-in-arithmetic"></span></p>
|
||
<p>Integers can be in bases other than 10. A leading ‘<code>0x</code>’ or ‘<code>0X</code>’
|
||
denotes hexadecimal and a leading ‘<code>0b</code>’ or ‘<code>0B</code>’ binary. Integers may
|
||
also be of the form ‘<code>base``#``n</code>’, where <code>base</code> is a decimal number
|
||
between two and thirty-six representing the arithmetic base and <code>n</code> is a
|
||
number in that base (for example, ‘<code>16#ff</code>’ is 255 in hexadecimal). The
|
||
<code>base``#</code> may also be omitted, in which case base 10 is used. For
|
||
backwards compatibility the form ‘<code>[``base``]``n</code>’ is also accepted.</p>
|
||
<p>An integer expression or a base given in the form ‘<code>base``#``n</code>’ may
|
||
contain underscores (‘<code>_</code>’) after the leading digit for visual guidance;
|
||
these are ignored in computation. Examples are <code>1_000_000</code> or
|
||
<code>0xffff_ffff</code> which are equivalent to <code>1000000</code> and <code>0xffffffff</code>
|
||
respectively.</p>
|
||
<p>It is also possible to specify a base to be used for output in the form
|
||
‘<code>[#``base``]</code>’, for example ‘<code>[#16]</code>’. This is used when outputting
|
||
arithmetical substitutions or when assigning to scalar parameters, but
|
||
an explicitly defined integer or floating point parameter will not be
|
||
affected. If an integer variable is implicitly defined by an arithmetic
|
||
expression, any base specified in this way will be set as the variable’s
|
||
output arithmetic base as if the option ‘<code>-i</code> <code>base</code>’ to the <code>typeset</code>
|
||
builtin had been used. The expression has no precedence and if it occurs
|
||
more than once in a mathematical expression, the last encountered is
|
||
used. For clarity it is recommended that it appear at the beginning of
|
||
an expression. As an example:</p>
|
||
<div class="example">
|
||
<pre><code class="language-example">typeset -i 16 y
|
||
print $(( [#8] x = 32, y = 32 ))
|
||
print $x $y
|
||
</code></pre>
|
||
</div>
|
||
<p>outputs first ‘<code>8#40</code>’, the rightmost value in the given output base,
|
||
and then ‘<code>8#40 16#20</code>’, because <code>y</code> has been explicitly declared to
|
||
have output base 16, while <code>x</code> (assuming it does not already exist) is
|
||
implicitly typed by the arithmetic evaluation, where it acquires the
|
||
output base 8.</p>
|
||
<p>The <code>base</code> may be replaced or followed by an underscore, which may
|
||
itself be followed by a positive integer (if it is missing the value 3
|
||
is used). This indicates that underscores should be inserted into the
|
||
output string, grouping the number for visual clarity. The following
|
||
integer specifies the number of digits to group together. For example:</p>
|
||
<div class="example">
|
||
<pre><code class="language-example">setopt cbases
|
||
print $(( [#16_4] 65536 ** 2 ))
|
||
</code></pre>
|
||
</div>
|
||
<p>outputs ‘<code>0x1_0000_0000</code>’.</p>
|
||
<p>The feature can be used with floating point numbers, in which case the
|
||
base must be omitted; grouping is away from the decimal point. For
|
||
example,</p>
|
||
<div class="example">
|
||
<pre><code class="language-example">zmodload zsh/mathfunc
|
||
print $(( [#_] sqrt(1e7) ))
|
||
</code></pre>
|
||
</div>
|
||
<p>outputs ‘<code>3_162.277_660_168_379_5</code>’ (the number of decimal places shown
|
||
may vary).</p>
|
||
<p><span id="index-C_005fBASES_002c-use-of"></span>
|
||
<span id="index-OCTAL_005fZEROES_002c-use-of"></span></p>
|
||
<p>If the <code>C_BASES</code> option is set, hexadecimal numbers are output in the
|
||
standard C format, for example ‘<code>0xFF</code>’ instead of the usual ‘<code>16#FF</code>’.
|
||
If the option <code>OCTAL_ZEROES</code> is also set (it is not by default), octal
|
||
numbers will be treated similarly and hence appear as ‘<code>077</code>’ instead of
|
||
‘<code>8#77</code>’. This option has no effect on the output of bases other than
|
||
hexadecimal and octal, and these formats are always understood on input.</p>
|
||
<p>When an output base is specified using the ‘<code>[#``base``]</code>’ syntax, an
|
||
appropriate base prefix will be output if necessary, so that the value
|
||
output is valid syntax for input. If the <code>#</code> is doubled, for example
|
||
‘<code>[##16]</code>’, then no base prefix is output.</p>
|
||
<p>Floating point constants are recognized by the presence of a decimal
|
||
point or an exponent. The decimal point may be the first character of
|
||
the constant, but the exponent character <code>e</code> or <code>E</code> may not, as it will
|
||
be taken for a parameter name. All numeric parts (before and after the
|
||
decimal point and in the exponent) may contain underscores after the
|
||
leading digit for visual guidance; these are ignored in computation.</p>
|
||
<p><span id="index-arithmetic-operators"></span>
|
||
<span id="index-operators_002c-arithmetic"></span></p>
|
||
<p>An arithmetic expression uses nearly the same syntax and associativity
|
||
of expressions as in C.</p>
|
||
<p>In the native mode of operation, the following operators are supported
|
||
(listed in decreasing order of precedence):</p>
|
||
<ul>
|
||
<li>
|
||
<p><code>+ - ! ~ ++ –</code><br />
|
||
unary plus/minus, logical NOT, complement, {pre,post}{in,de}crement</p>
|
||
</li>
|
||
<li>
|
||
<p><code><< >></code><br />
|
||
bitwise shift left, right</p>
|
||
</li>
|
||
<li>
|
||
<p><code>&</code><br />
|
||
bitwise AND</p>
|
||
</li>
|
||
<li>
|
||
<p><code>^</code><br />
|
||
bitwise XOR</p>
|
||
</li>
|
||
<li>
|
||
<p><code>|</code><br />
|
||
bitwise OR</p>
|
||
</li>
|
||
<li>
|
||
<p><code>**</code><br />
|
||
exponentiation</p>
|
||
</li>
|
||
<li>
|
||
<p><code>* / %</code><br />
|
||
multiplication, division, modulus (remainder)</p>
|
||
</li>
|
||
<li>
|
||
<p><code>+ -</code><br />
|
||
addition, subtraction</p>
|
||
</li>
|
||
<li>
|
||
<p><code>< > <= >=</code><br />
|
||
comparison</p>
|
||
</li>
|
||
<li>
|
||
<p><code>== !=</code><br />
|
||
equality and inequality</p>
|
||
</li>
|
||
<li>
|
||
<p><code>&&</code><br />
|
||
logical AND</p>
|
||
</li>
|
||
<li>
|
||
<p><code>|| ^^</code><br />
|
||
logical OR, XOR</p>
|
||
</li>
|
||
<li>
|
||
<p><code>? :</code><br />
|
||
ternary operator</p>
|
||
</li>
|
||
<li>
|
||
<p><code>= += -= *= /= %= &= ^= |= <<= >>= &&= ||= ^^= **=</code><br />
|
||
assignment</p>
|
||
</li>
|
||
<li>
|
||
<p><code>,</code><br />
|
||
comma operator</p>
|
||
</li>
|
||
</ul>
|
||
<p>The operators ‘<code>&&</code>’, ‘<code>||</code>’, ‘<code>&&=</code>’, and ‘<code>||=</code>’ are short-circuiting,
|
||
and only one of the latter two expressions in a ternary operator is
|
||
evaluated. Note the precedence of the bitwise AND, OR, and XOR
|
||
operators.</p>
|
||
<p>With the option <code>C_PRECEDENCES</code> the precedences (but no other
|
||
properties) of the operators are altered to be the same as those in most
|
||
other languages that support the relevant operators:</p>
|
||
<ul>
|
||
<li>
|
||
<p><code>+ - ! ~ ++ –</code><br />
|
||
unary plus/minus, logical NOT, complement, {pre,post}{in,de}crement</p>
|
||
</li>
|
||
<li>
|
||
<p><code>**</code><br />
|
||
exponentiation</p>
|
||
</li>
|
||
<li>
|
||
<p><code>* / %</code><br />
|
||
multiplication, division, modulus (remainder)</p>
|
||
</li>
|
||
<li>
|
||
<p><code>+ -</code><br />
|
||
addition, subtraction</p>
|
||
</li>
|
||
<li>
|
||
<p><code><< >></code><br />
|
||
bitwise shift left, right</p>
|
||
</li>
|
||
<li>
|
||
<p><code>< > <= >=</code><br />
|
||
comparison</p>
|
||
</li>
|
||
<li>
|
||
<p><code>== !=</code><br />
|
||
equality and inequality</p>
|
||
</li>
|
||
<li>
|
||
<p><code>&</code><br />
|
||
bitwise AND</p>
|
||
</li>
|
||
<li>
|
||
<p><code>^</code><br />
|
||
bitwise XOR</p>
|
||
</li>
|
||
<li>
|
||
<p><code>|</code><br />
|
||
bitwise OR</p>
|
||
</li>
|
||
<li>
|
||
<p><code>&&</code><br />
|
||
logical AND</p>
|
||
</li>
|
||
<li>
|
||
<p><code>^^</code><br />
|
||
logical XOR</p>
|
||
</li>
|
||
<li>
|
||
<p><code>||</code><br />
|
||
logical OR</p>
|
||
</li>
|
||
<li>
|
||
<p><code>? :</code><br />
|
||
ternary operator</p>
|
||
</li>
|
||
<li>
|
||
<p><code>= += -= *= /= %= &= ^= |= <<= >>= &&= ||= ^^= **=</code><br />
|
||
assignment</p>
|
||
</li>
|
||
<li>
|
||
<p><code>,</code><br />
|
||
comma operator</p>
|
||
</li>
|
||
</ul>
|
||
<p>Note the precedence of exponentiation in both cases is below that of
|
||
unary operators, hence ‘<code>-3**2</code>’ evaluates as ‘<code>9</code>’, not ‘<code>-9</code>’. Use
|
||
parentheses where necessary: ‘<code>-(3**2)</code>’. This is for compatibility with
|
||
other shells.</p>
|
||
<p><span id="index-mathematical-functions_002c-use-of"></span>
|
||
<span id="index-functions_002c-math_002c-use-of"></span></p>
|
||
<p>Mathematical functions can be called with the syntax
|
||
‘<code>func``(``args``)</code>’, where the function decides if the <code>args</code> is
|
||
used as a string or a comma-separated list of arithmetic expressions.
|
||
The shell currently defines no mathematical functions by default, but
|
||
the module <code>zsh/mathfunc</code> may be loaded with the <code>zmodload</code> builtin to
|
||
provide standard floating point mathematical functions.</p>
|
||
<p>An expression of the form ‘<code>##``x</code>’ where <code>x</code> is any character sequence
|
||
such as ‘<code>a</code>’, ‘<code>^A</code>’, or ‘<code>\M-\C-x</code>’ gives the value of this character
|
||
and an expression of the form ‘<code>#``name</code>’ gives the value of the first
|
||
character of the contents of the parameter <code>name</code>. Character values are
|
||
according to the character set used in the current locale; for multibyte
|
||
character handling the option <code>MULTIBYTE</code> must be set. Note that this
|
||
form is different from ‘<code>$#``name</code>’, a standard parameter substitution
|
||
which gives the length of the parameter <code>name</code>. ‘<code>#\</code>’ is accepted
|
||
instead of ‘<code>##</code>’, but its use is deprecated.</p>
|
||
<p>Named parameters and subscripted arrays can be referenced by name within
|
||
an arithmetic expression without using the parameter expansion syntax.
|
||
For example,</p>
|
||
<div class="example">
|
||
<pre><code class="language-example">((val2 = val1 * 2))
|
||
</code></pre>
|
||
</div>
|
||
<p>assigns twice the value of <code>$val1</code> to the parameter named <code>val2</code>.</p>
|
||
<p>An internal integer representation of a named parameter can be specified
|
||
with the <code>integer</code> builtin.
|
||
<span id="index-parameters_002c-integer"></span>
|
||
<span id="index-integer-parameters"></span>
|
||
<span id="index-integer_002c-use-of"></span> Arithmetic evaluation is
|
||
performed on the value of each assignment to a named parameter declared
|
||
integer in this manner. Assigning a floating point number to an integer
|
||
results in rounding towards zero.</p>
|
||
<p><span id="index-parameters_002c-floating-point"></span>
|
||
<span id="index-floating-point-parameters"></span>
|
||
<span id="index-float_002c-use-of"></span></p>
|
||
<p>Likewise, floating point numbers can be declared with the <code>float</code>
|
||
builtin; there are two types, differing only in their output format, as
|
||
described for the <code>typeset</code> builtin. The output format can be bypassed
|
||
by using arithmetic substitution instead of the parameter substitution,
|
||
i.e. ‘<code>${``float``}</code>’ uses the defined format, but ‘<code>$((``float``))</code>’
|
||
uses a generic floating point format.</p>
|
||
<p>Promotion of integer to floating point values is performed where
|
||
necessary. In addition, if any operator which requires an integer
|
||
(‘<code>&</code>’, ‘<code>|</code>’, ‘<code>^</code>’, ‘<code><<</code>’, ‘<code>>></code>’ and their equivalents with
|
||
assignment) is given a floating point argument, it will be silently
|
||
rounded towards zero except for ‘<code>~</code>’ which rounds down.</p>
|
||
<p>Users should beware that, in common with many other programming
|
||
languages but not software designed for calculation, the evaluation of
|
||
an expression in zsh is taken a term at a time and promotion of integers
|
||
to floating point does not occur in terms only containing integers. A
|
||
typical result of this is that a division such as <code>6/8</code> is truncated, in
|
||
this being rounded towards 0. The <code>FORCE_FLOAT</code> shell option can be used
|
||
in scripts or functions where floating point evaluation is required
|
||
throughout.</p>
|
||
<p>Scalar variables can hold integer or floating point values at different
|
||
times; there is no memory of the numeric type in this case.</p>
|
||
<p>If a variable is first assigned in a numeric context without previously
|
||
being declared, it will be implicitly typed as <code>integer</code> or <code>float</code> and
|
||
retain that type either until the type is explicitly changed or until
|
||
the end of the scope. This can have unforeseen consequences. For
|
||
example, in the loop</p>
|
||
<div class="example">
|
||
<pre><code class="language-example">for (( f = 0; f < 1; f += 0.1 )); do
|
||
# use $f
|
||
done
|
||
</code></pre>
|
||
</div>
|
||
<p>if <code>f</code> has not already been declared, the first assignment will cause it
|
||
to be created as an integer, and consequently the operation ‘<code>f += 0.1</code>’
|
||
will always cause the result to be truncated to zero, so that the loop
|
||
will fail. A simple fix would be to turn the initialization into ‘<code>f = 0.0</code>’. It is therefore best to declare numeric variables with explicit
|
||
types.</p>
|
||
<hr />
|
||
<p>This document was generated on <em>February 15, 2020</em> using
|
||
<a href="http://www.nongnu.org/texi2html/"><em>texi2html 5.0</em></a>.<br />
|
||
Zsh version 5.8, released on February 14, 2020.</p>
|
||
|
||
</main>
|
||
|
||
<nav class="nav-wrapper" aria-label="Page navigation">
|
||
<!-- Mobile navigation buttons -->
|
||
|
||
<a rel="prev" href="Jobs-_0026-Signals.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
||
<i class="fa fa-angle-left"></i>
|
||
</a>
|
||
|
||
|
||
|
||
<a rel="next" href="Conditional-Expressions.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
||
<i class="fa fa-angle-right"></i>
|
||
</a>
|
||
|
||
|
||
<div style="clear: both"></div>
|
||
</nav>
|
||
</div>
|
||
</div>
|
||
|
||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
||
|
||
<a rel="prev" href="Jobs-_0026-Signals.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
||
<i class="fa fa-angle-left"></i>
|
||
</a>
|
||
|
||
|
||
|
||
<a rel="next" href="Conditional-Expressions.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
||
<i class="fa fa-angle-right"></i>
|
||
</a>
|
||
|
||
</nav>
|
||
|
||
</div>
|
||
|
||
|
||
<!-- Livereload script (if served using the cli tool) -->
|
||
<script type="text/javascript">
|
||
var socket = new WebSocket("ws://localhost:3000/__livereload");
|
||
socket.onmessage = function (event) {
|
||
if (event.data === "reload") {
|
||
socket.close();
|
||
location.reload();
|
||
}
|
||
};
|
||
|
||
window.onbeforeunload = function() {
|
||
socket.close();
|
||
}
|
||
</script>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<script type="text/javascript">
|
||
window.playground_copyable = true;
|
||
</script>
|
||
|
||
|
||
|
||
|
||
|
||
<script src="elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
|
||
<script src="mark.min.js" type="text/javascript" charset="utf-8"></script>
|
||
<script src="searcher.js" type="text/javascript" charset="utf-8"></script>
|
||
|
||
|
||
<script src="clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
||
<script src="highlight.js" type="text/javascript" charset="utf-8"></script>
|
||
<script src="book.js" type="text/javascript" charset="utf-8"></script>
|
||
|
||
<!-- Custom JS scripts -->
|
||
|
||
|
||
|
||
|
||
</body>
|
||
</html>
|