zsh-manual-mdbook/zsh_manual/book/Arithmetic-Evaluation.html

490 lines
27 KiB
HTML
Raw Permalink Normal View History

2021-05-11 02:29:21 +02:00
<!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 -->
2022-09-01 07:20:37 +02:00
<link rel="stylesheet" href="./theme/catppuccin.css">
<link rel="stylesheet" href="./theme/catppuccin-highlight.css">
2021-05-11 02:29:21 +02:00
</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">
2022-08-24 15:22:41 +02:00
<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-&-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>
2021-05-11 02:29:21 +02:00
</div>
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
2022-08-24 15:22:41 +02:00
<div id="menu-bar-hover-placeholder"></div>
2021-05-11 02:29:21 +02:00
<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>
2022-09-01 07:20:37 +02:00
<li role="none"><button role="menuitem" class="theme" id="latte">Latte</button></li>
<li role="none"><button role="menuitem" class="theme" id="frappe">Frappé</button></li>
<li role="none"><button role="menuitem" class="theme" id="macchiato">Macchiato</button></li>
<li role="none"><button role="menuitem" class="theme" id="mocha">Mocha</button></li>
2021-05-11 02:29:21 +02:00
</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>
2021-05-17 17:00:52 +02:00
2021-05-11 02:29:21 +02:00
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
2021-05-17 17:00:52 +02:00
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
2021-05-11 02:29:21 +02:00
</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 -->
2022-08-24 15:22:41 +02:00
<p><span id="Arithmetic-Evaluation"></span> <span
id="Arithmetic-Evaluation-1"></span></p>
2021-05-11 02:29:21 +02:00
<h1 id="11-arithmetic-evaluation"><a class="header" href="#11-arithmetic-evaluation">11 Arithmetic Evaluation</a></h1>
2022-08-24 15:22:41 +02:00
<p><span id="index-arithmetic-evaluation"></span> <span
id="index-evaluation_002c-arithmetic"></span> <span
id="index-let_002c-use-of"></span></p>
2021-05-11 02:29:21 +02:00
<p>The shell can perform integer and floating point arithmetic, either
2022-08-24 15:22:41 +02:00
using the builtin let, 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 print - $(( 12345678901 )); 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 let 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 ((, all the characters until a matching
)) are treated as a double-quoted expression and arithmetic expansion
performed as for an argument of let. More precisely, ((<code>...</code>)) is
equivalent to let &quot;<code>...</code>&quot;. 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>
2021-05-11 02:29:21 +02:00
<p>For example, the following statement</p>
<div class="example">
2022-08-24 15:22:41 +02:00
<pre><code class="language-zsh">(( val = 2 + 1 ))
2021-05-11 02:29:21 +02:00
</code></pre>
</div>
<p>is equivalent to</p>
<div class="example">
2022-08-24 15:22:41 +02:00
<pre><code class="language-zsh">let &quot;val = 2 + 1&quot;
2021-05-11 02:29:21 +02:00
</code></pre>
</div>
2022-08-24 15:22:41 +02:00
<p>both assigning the value 3 to the shell variable val and returning a
2021-05-11 02:29:21 +02:00
zero status.</p>
2022-08-24 15:22:41 +02:00
<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 0x or 0X denotes
hexadecimal and a leading 0b or 0B binary. Integers may also be of
the form <code>base</code>#<code>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, 16#ff 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</code>]<code>n</code> is also accepted.</p>
<p>An integer expression or a base given in the form <code>base</code>#<code>n</code> may
contain underscores (_) after the leading digit for visual guidance;
these are ignored in computation. Examples are 1_000_000 or 0xffff_ffff
which are equivalent to 1000000 and 0xffffffff respectively.</p>
2021-05-11 02:29:21 +02:00
<p>It is also possible to specify a base to be used for output in the form
2022-08-24 15:22:41 +02:00
[#<code>base</code>], for example [#16]. This is used when outputting
2021-05-11 02:29:21 +02:00
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 variables
2022-08-24 15:22:41 +02:00
output arithmetic base as if the option -i <code>base</code> to the typeset
2021-05-11 02:29:21 +02:00
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">
2022-08-24 15:22:41 +02:00
<pre><code class="language-zsh">typeset -i 16 y
2021-05-11 02:29:21 +02:00
print $(( [#8] x = 32, y = 32 ))
print $x $y
</code></pre>
</div>
2022-08-24 15:22:41 +02:00
<p>outputs first 8#40, the rightmost value in the given output base, and
then 8#40 16#20, because y has been explicitly declared to have output
base 16, while x (assuming it does not already exist) is implicitly
typed by the arithmetic evaluation, where it acquires the output base 8.</p>
2021-05-11 02:29:21 +02:00
<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">
2022-08-24 15:22:41 +02:00
<pre><code class="language-zsh">setopt cbases
2021-05-11 02:29:21 +02:00
print $(( [#16_4] 65536 ** 2 ))
</code></pre>
</div>
2022-08-24 15:22:41 +02:00
<p>outputs 0x1_0000_0000.</p>
2021-05-11 02:29:21 +02:00
<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">
2022-08-24 15:22:41 +02:00
<pre><code class="language-zsh">zmodload zsh/mathfunc
2021-05-11 02:29:21 +02:00
print $(( [#_] sqrt(1e7) ))
</code></pre>
</div>
2022-08-24 15:22:41 +02:00
<p>outputs 3_162.277_660_168_379_5 (the number of decimal places shown
2021-05-11 02:29:21 +02:00
may vary).</p>
2022-08-24 15:22:41 +02:00
<p><span id="index-C_005fBASES_002c-use-of"></span> <span
id="index-OCTAL_005fZEROES_002c-use-of"></span></p>
<p>If the C_BASES option is set, hexadecimal numbers are output in the
standard C format, for example 0xFF instead of the usual 16#FF. If
the option OCTAL_ZEROES is also set (it is not by default), octal
numbers will be treated similarly and hence appear as 077 instead of
8#77. This option has no effect on the output of bases other than
2021-05-11 02:29:21 +02:00
hexadecimal and octal, and these formats are always understood on input.</p>
2022-08-24 15:22:41 +02:00
<p>When an output base is specified using the [#<code>base</code>] syntax, an
2021-05-11 02:29:21 +02:00
appropriate base prefix will be output if necessary, so that the value
2022-08-24 15:22:41 +02:00
output is valid syntax for input. If the # is doubled, for example
[##16], then no base prefix is output.</p>
2021-05-11 02:29:21 +02:00
<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
2022-08-24 15:22:41 +02:00
the constant, but the exponent character e or E may not, as it will be
taken for a parameter name. All numeric parts (before and after the
2021-05-11 02:29:21 +02:00
decimal point and in the exponent) may contain underscores after the
leading digit for visual guidance; these are ignored in computation.</p>
2022-08-24 15:22:41 +02:00
<p><span id="index-arithmetic-operators"></span> <span
id="index-operators_002c-arithmetic"></span></p>
2021-05-11 02:29:21 +02:00
<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>
2022-08-24 15:22:41 +02:00
<p>+ - ! ~ ++ <br />
2021-05-11 02:29:21 +02:00
unary plus/minus, logical NOT, complement, {pre,post}{in,de}crement</p>
2022-08-24 15:22:41 +02:00
<p>&lt;&lt; &gt;&gt;<br />
2021-05-11 02:29:21 +02:00
bitwise shift left, right</p>
2022-08-24 15:22:41 +02:00
<p>&amp;<br />
2021-05-11 02:29:21 +02:00
bitwise AND</p>
2022-08-24 15:22:41 +02:00
<p>^<br />
2021-05-11 02:29:21 +02:00
bitwise XOR</p>
2022-08-24 15:22:41 +02:00
<p>|<br />
2021-05-11 02:29:21 +02:00
bitwise OR</p>
2022-08-24 15:22:41 +02:00
<p>**<br />
2021-05-11 02:29:21 +02:00
exponentiation</p>
2022-08-24 15:22:41 +02:00
<p>* / %<br />
2021-05-11 02:29:21 +02:00
multiplication, division, modulus (remainder)</p>
2022-08-24 15:22:41 +02:00
<p>+ -<br />
2021-05-11 02:29:21 +02:00
addition, subtraction</p>
2022-08-24 15:22:41 +02:00
<p>&lt; &gt; &lt;= &gt;=<br />
2021-05-11 02:29:21 +02:00
comparison</p>
2022-08-24 15:22:41 +02:00
<p>== !=<br />
2021-05-11 02:29:21 +02:00
equality and inequality</p>
2022-08-24 15:22:41 +02:00
<p>&amp;&amp;<br />
2021-05-11 02:29:21 +02:00
logical AND</p>
2022-08-24 15:22:41 +02:00
<p>|| ^^<br />
2021-05-11 02:29:21 +02:00
logical OR, XOR</p>
2022-08-24 15:22:41 +02:00
<p>? :<br />
2021-05-11 02:29:21 +02:00
ternary operator</p>
2022-08-24 15:22:41 +02:00
<p>= += -= *= /= %= &amp;= ^= |= &lt;&lt;= &gt;&gt;= &amp;&amp;= ||= ^^= **=<br />
2021-05-11 02:29:21 +02:00
assignment</p>
2022-08-24 15:22:41 +02:00
<p>,<br />
2021-05-11 02:29:21 +02:00
comma operator</p>
2022-08-24 15:22:41 +02:00
<p>The operators &amp;&amp;, ||, &amp;&amp;=, and ||= are short-circuiting, and
only one of the latter two expressions in a ternary operator is
2021-05-11 02:29:21 +02:00
evaluated. Note the precedence of the bitwise AND, OR, and XOR
operators.</p>
2022-08-24 15:22:41 +02:00
<p>With the option C_PRECEDENCES 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>
<p>+ - ! ~ ++ <br />
2021-05-11 02:29:21 +02:00
unary plus/minus, logical NOT, complement, {pre,post}{in,de}crement</p>
2022-08-24 15:22:41 +02:00
<p>**<br />
2021-05-11 02:29:21 +02:00
exponentiation</p>
2022-08-24 15:22:41 +02:00
<p>* / %<br />
2021-05-11 02:29:21 +02:00
multiplication, division, modulus (remainder)</p>
2022-08-24 15:22:41 +02:00
<p>+ -<br />
2021-05-11 02:29:21 +02:00
addition, subtraction</p>
2022-08-24 15:22:41 +02:00
<p>&lt;&lt; &gt;&gt;<br />
2021-05-11 02:29:21 +02:00
bitwise shift left, right</p>
2022-08-24 15:22:41 +02:00
<p>&lt; &gt; &lt;= &gt;=<br />
2021-05-11 02:29:21 +02:00
comparison</p>
2022-08-24 15:22:41 +02:00
<p>== !=<br />
2021-05-11 02:29:21 +02:00
equality and inequality</p>
2022-08-24 15:22:41 +02:00
<p>&amp;<br />
2021-05-11 02:29:21 +02:00
bitwise AND</p>
2022-08-24 15:22:41 +02:00
<p>^<br />
2021-05-11 02:29:21 +02:00
bitwise XOR</p>
2022-08-24 15:22:41 +02:00
<p>|<br />
2021-05-11 02:29:21 +02:00
bitwise OR</p>
2022-08-24 15:22:41 +02:00
<p>&amp;&amp;<br />
2021-05-11 02:29:21 +02:00
logical AND</p>
2022-08-24 15:22:41 +02:00
<p>^^<br />
2021-05-11 02:29:21 +02:00
logical XOR</p>
2022-08-24 15:22:41 +02:00
<p>||<br />
2021-05-11 02:29:21 +02:00
logical OR</p>
2022-08-24 15:22:41 +02:00
<p>? :<br />
2021-05-11 02:29:21 +02:00
ternary operator</p>
2022-08-24 15:22:41 +02:00
<p>= += -= *= /= %= &amp;= ^= |= &lt;&lt;= &gt;&gt;= &amp;&amp;= ||= ^^= **=<br />
2021-05-11 02:29:21 +02:00
assignment</p>
2022-08-24 15:22:41 +02:00
<p>,<br />
2021-05-11 02:29:21 +02:00
comma operator</p>
<p>Note the precedence of exponentiation in both cases is below that of
2022-08-24 15:22:41 +02:00
unary operators, hence -3**2 evaluates as 9, not -9. Use
parentheses where necessary: -(3**2). This is for compatibility with
2021-05-11 02:29:21 +02:00
other shells.</p>
2022-08-24 15:22:41 +02:00
<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</code>(<code>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
zsh/mathfunc may be loaded with the zmodload 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 a, ^A, or \M-\C-x gives the value of this character and
an expression of the form #<code>name</code> gives the value of the first
2021-05-11 02:29:21 +02:00
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
2022-08-24 15:22:41 +02:00
character handling the option MULTIBYTE 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>. #\ is accepted instead of
##, but its use is deprecated.</p>
2021-05-11 02:29:21 +02:00
<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">
2022-08-24 15:22:41 +02:00
<pre><code class="language-zsh">((val2 = val1 * 2))
2021-05-11 02:29:21 +02:00
</code></pre>
</div>
2022-08-24 15:22:41 +02:00
<p>assigns twice the value of $val1 to the parameter named val2.</p>
2021-05-11 02:29:21 +02:00
<p>An internal integer representation of a named parameter can be specified
2022-08-24 15:22:41 +02:00
with the integer 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
2021-05-11 02:29:21 +02:00
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>
2022-08-24 15:22:41 +02:00
<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 float builtin;
there are two types, differing only in their output format, as described
for the typeset 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>
2021-05-11 02:29:21 +02:00
<p>Promotion of integer to floating point values is performed where
2022-08-24 15:22:41 +02:00
necessary. In addition, if any operator which requires an integer (&amp;,
|, ^, &lt;&lt;, &gt;&gt; and their equivalents with assignment) is given
a floating point argument, it will be silently rounded towards zero
except for ~ which rounds down.</p>
2021-05-11 02:29:21 +02:00
<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
2022-08-24 15:22:41 +02:00
typical result of this is that a division such as 6/8 is truncated, in
this being rounded towards 0. The FORCE_FLOAT shell option can be used
2021-05-11 02:29:21 +02:00
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
2022-08-24 15:22:41 +02:00
being declared, it will be implicitly typed as integer or float and
2021-05-11 02:29:21 +02:00
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">
2022-08-24 15:22:41 +02:00
<pre><code class="language-zsh">for (( f = 0; f &lt; 1; f += 0.1 )); do
2021-05-11 02:29:21 +02:00
# use $f
done
</code></pre>
</div>
2022-08-24 15:22:41 +02:00
<p>if f has not already been declared, the first assignment will cause it
to be created as an integer, and consequently the operation f += 0.1
2021-05-11 02:29:21 +02:00
will always cause the result to be truncated to zero, so that the loop
2022-08-24 15:22:41 +02:00
will fail. A simple fix would be to turn the initialization into f =
0.0. It is therefore best to declare numeric variables with explicit
2021-05-11 02:29:21 +02:00
types.</p>
<hr />
2022-08-24 15:22:41 +02:00
<p>This document was generated on <em>May 14, 2022</em> using <a href="http://www.nongnu.org/texi2html/"><em>texi2html
5.0</em></a>.<br />
Zsh version 5.9, released on May 14, 2022.</p>
2021-05-11 02:29:21 +02:00
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
2022-08-24 15:22:41 +02:00
<a rel="prev" href="Jobs-&amp;-Signals.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
2021-05-11 02:29:21 +02:00
<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">
2022-08-24 15:22:41 +02:00
<a rel="prev" href="Jobs-&amp;-Signals.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
2021-05-11 02:29:21 +02:00
<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>
2022-09-01 07:20:37 +02:00
<!-- Livereload script (if served using the cli tool) -->
<script type="text/javascript">
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};
window.onbeforeunload = function() {
socket.close();
}
</script>
2021-05-11 02:29:21 +02:00
<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>