1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>The zash Shell Language</title><meta name="authors" content="Sean Kurtz"><style>/* Alignment */
mtable.right-align mtd,
mtable mtd.right-align,
mtable.left-align mtd.right-align,
mtable.aligned mtd:nth-child(odd) {
justify-items: end;
text-align: right;
}
mtable.cases mtd,
mtable.left-align mtd,
mtable mtd.left-align,
mtable.aligned mtd:nth-child(even),
math:is(:not([display])) > mtable.multiline-equation mtd {
justify-items: start;
text-align: left;
}
mtable.cases mtd,
mtable.aligned mtd,
mtable mtd.flushed,
mtable mtd.left-flush {
padding-left: 0;
}
mtable.cases mtd,
mtable.aligned mtd,
mtable mtd.flushed,
mtable mtd.right-flush {
padding-right: 0;
}
/* Tables */
mtable {
math-style: inherit;
}
mtd {
math-depth: auto-add;
math-style: compact;
math-shift: compact;
}
/* Equations */
mtable.multiline-equation mtd {
math-depth: inherit;
math-style: inherit;
math-shift: inherit;
padding: 0;
}
math > mtable.multiline-equation mtr:not(:last-child) mtd {
padding-bottom: 0.5em;
}
/* Fractions */
mfrac {
padding-inline: 0;
margin-inline: 0.1em;
}
/* Accents */
mover[accent="true" i] > :first-child {
font-feature-settings: "dtls";
}
mover.dotted[accent="true" i] > :first-child {
font-feature-settings: "dtls" 0;
}
/* Other rules for scriptlevel, displaystyle and math-shift */
munder > :nth-child(2),
munderover > :nth-child(2) {
math-shift: compact
}
munder[accentunder="true" i] > :not(:first-child),
mover[accent="true" i] > :not(:first-child) {
math-depth: inherit;
math-style: inherit;
math-shift: inherit;
}</style></head><body><div>zash is a small, fast POSIX shell for z/OS UNIX System Services in the Almquist tradition. This document specifies the language it accepts and what each construct means, as numbered rules that the test suite cites and that a later machine-checked model can follow one for one.</div><nav role="doc-toc"><h2>Contents</h2><ol style="list-style-type: none"><li><div><span style="text-decoration: underline"><a href="#loc-1"><span class="prefix">1</span> Introduction</a></span></div><ol style="list-style-type: none"><li><span style="text-decoration: underline"><a href="#loc-2"><span class="prefix">1.1</span> Scope</a></span></li><li><span style="text-decoration: underline"><a href="#loc-3"><span class="prefix">1.2</span> Notation</a></span></li><li><span style="text-decoration: underline"><a href="#loc-4"><span class="prefix">1.3</span> Rule identifiers</a></span></li></ol></li><li><div><span style="text-decoration: underline"><a href="#lexical"><span class="prefix">2</span> Lexical structure</a></span></div><ol style="list-style-type: none"><li><span style="text-decoration: underline"><a href="#loc-5"><span class="prefix">2.1</span> Input</a></span></li><li><span style="text-decoration: underline"><a href="#loc-6"><span class="prefix">2.2</span> Tokens</a></span></li><li><span style="text-decoration: underline"><a href="#loc-7"><span class="prefix">2.3</span> Quoting</a></span></li><li><span style="text-decoration: underline"><a href="#reserved"><span class="prefix">2.4</span> Reserved words</a></span></li><li><span style="text-decoration: underline"><a href="#loc-8"><span class="prefix">2.5</span> Here-documents</a></span></li></ol></li><li><span style="text-decoration: underline"><a href="#aliases"><span class="prefix">3</span> Aliases</a></span></li><li><div><span style="text-decoration: underline"><a href="#grammar"><span class="prefix">4</span> Grammar</a></span></div><ol style="list-style-type: none"><li><span style="text-decoration: underline"><a href="#loc-9"><span class="prefix">4.1</span> Productions</a></span></li><li><span style="text-decoration: underline"><a href="#loc-10"><span class="prefix">4.2</span> Rules attached to the grammar</a></span></li></ol></li><li><div><span style="text-decoration: underline"><a href="#expansion"><span class="prefix">5</span> Word expansion</a></span></div><ol style="list-style-type: none"><li><span style="text-decoration: underline"><a href="#loc-11"><span class="prefix">5.1</span> Words and fields</a></span></li><li><span style="text-decoration: underline"><a href="#loc-12"><span class="prefix">5.2</span> Tilde expansion</a></span></li><li><span style="text-decoration: underline"><a href="#loc-13"><span class="prefix">5.3</span> Parameter expansion</a></span></li><li><span style="text-decoration: underline"><a href="#loc-14"><span class="prefix">5.4</span> Command substitution</a></span></li><li><span style="text-decoration: underline"><a href="#loc-15"><span class="prefix">5.5</span> Arithmetic expansion</a></span></li><li><span style="text-decoration: underline"><a href="#loc-16"><span class="prefix">5.6</span> Field splitting</a></span></li><li><span style="text-decoration: underline"><a href="#loc-17"><span class="prefix">5.7</span> Pathname expansion</a></span></li></ol></li><li><span style="text-decoration: underline"><a href="#patterns"><span class="prefix">6</span> Patterns</a></span></li><li><div><span style="text-decoration: underline"><a href="#arithmetic"><span class="prefix">7</span> Arithmetic</a></span></div><ol style="list-style-type: none"><li><span style="text-decoration: underline"><a href="#loc-18"><span class="prefix">7.1</span> Syntax</a></span></li><li><span style="text-decoration: underline"><a href="#loc-19"><span class="prefix">7.2</span> Values</a></span></li></ol></li><li><div><span style="text-decoration: underline"><a href="#commands"><span class="prefix">8</span> Commands</a></span></div><ol style="list-style-type: none"><li><span style="text-decoration: underline"><a href="#loc-20"><span class="prefix">8.1</span> Exit status</a></span></li><li><span style="text-decoration: underline"><a href="#loc-21"><span class="prefix">8.2</span> Simple commands</a></span></li><li><span style="text-decoration: underline"><a href="#loc-22"><span class="prefix">8.3</span> Pipelines</a></span></li><li><span style="text-decoration: underline"><a href="#loc-23"><span class="prefix">8.4</span> Lists</a></span></li><li><span style="text-decoration: underline"><a href="#loc-24"><span class="prefix">8.5</span> Compound commands</a></span></li><li><span style="text-decoration: underline"><a href="#loc-25"><span class="prefix">8.6</span> Functions</a></span></li></ol></li><li><span style="text-decoration: underline"><a href="#conditional"><span class="prefix">9</span> Conditional expressions <span style="display: inline-block">[[ ]]</span></a></span></li><li><span style="text-decoration: underline"><a href="#redirection"><span class="prefix">10</span> Redirection</a></span></li><li><span style="text-decoration: underline"><a href="#environment"><span class="prefix">11</span> Environment and variables</a></span></li><li><span style="text-decoration: underline"><a href="#errors"><span class="prefix">12</span> Errors</a></span></li><li><span style="text-decoration: underline"><a href="#traps"><span class="prefix">13</span> Traps and signals</a></span></li><li><span style="text-decoration: underline"><a href="#zos"><span class="prefix">14</span> z/OS</a></span></li><li><span style="text-decoration: underline"><a href="#differences"><span class="prefix">15</span> Differences from POSIX and other shells</a></span></li><li><span style="text-decoration: underline"><a href="#loc-26"><span class="prefix">16</span> Mapping to a formal model</a></span></li></ol></nav><h2 id="loc-1">1 Introduction</h2><p>This document specifies the language that zash accepts and what each construct means. It is the reference for script writers and zash reviewers. Each rule has an identifier that tests cite, and a later Lean formalisation will encode each rule as a definition or a theorem.</p><p>The base is the Shell Command Language of POSIX.1-2024 (XCU chapter 2). Where this document is silent, POSIX applies. Where zash differs from POSIX or extends it, the rule says so and is listed again in <span style="text-decoration: underline"><a href="#differences">Section 15</a></span>.</p><h3 id="loc-2">1.1 Scope</h3><p>In scope: the input language (lexical structure and grammar), word expansion, the meaning of commands, redirections, the environment a command runs in, exit statuses and errors.</p><p>Out of scope: the interactive line editor, history and completion; the behaviour of individual builtins beyond what the language depends on; z/OS file tagging and character conversion, which affect the bytes a file holds but not what a script means (see <span style="text-decoration: underline"><a href="#zos">Section 14</a></span>).</p><h3 id="loc-3">1.2 Notation</h3><ul><li>Grammar productions are in EBNF: <span style="display: inline-block">a b</span> is sequence, <span style="display: inline-block">a | b</span> alternation, <span style="display: inline-block">[ a ]</span> optional, <span style="display: inline-block">{ a }</span> zero or more, <span style="display: inline-block">'x'</span> a literal token. Terminals in capitals (<span style="display: inline-block">WORD</span>, <span style="display: inline-block">NAME</span>, <span style="display: inline-block">NEWLINE</span>) are defined in <span style="text-decoration: underline"><a href="#lexical">Section 2</a></span>.</li><li><math><mrow><mo>⟦</mo><mi>𝑒</mi><mo>⟧</mo></mrow></math> is the meaning of the phrase <math><mi>𝑒</mi></math>: the value of an arithmetic expression, or the list of fields a word expands to.</li><li>A <em>byte</em> is an octet of the input. zash is built for ASCII or for IBM-1047 (EBCDIC); every character named in this document by its glyph (such as <span style="display: inline-block">$</span> or newline) stands for that character’s code in the build’s character set. No rule depends on the numeric value of a code unless it says so.</li><li>Numbered rules are normative. <em>Note</em> blocks are not.</li><li><strong>Open</strong> blocks mark questions this draft has not decided. Each must be answered by reading the implementation or by a test before the rule it belongs to is final.</li></ul><h3 id="loc-4">1.3 Rule identifiers</h3><p>Rules are numbered <span style="display: inline-block">R-</span><em>chapter</em><span style="display: inline-block">-</span><em>n</em>. The chapter codes are <span style="display: inline-block">LEX</span> (lexical structure), <span style="display: inline-block">ALI</span> (aliases), <span style="display: inline-block">GRM</span> (grammar), <span style="display: inline-block">EXP</span> (word expansion), <span style="display: inline-block">ARI</span> (arithmetic), <span style="display: inline-block">PAT</span> (patterns), <span style="display: inline-block">CMD</span> (commands), <span style="display: inline-block">CND</span> (conditional expressions), <span style="display: inline-block">RED</span> (redirection), <span style="display: inline-block">ENV</span> (environment and variables), <span style="display: inline-block">ERR</span> (errors) and <span style="display: inline-block">SIG</span> (traps). An identifier, once published, is not reused for a different statement.</p><h2 id="lexical">2 Lexical structure</h2><h3 id="loc-5">2.1 Input</h3><div>R-LEX-1 The input is a sequence of bytes, read from a file, a string (<span style="display: inline-block">-c</span>, <span style="display: inline-block">eval</span>, command substitution) or standard input. The byte value 0 (NUL) ends a line’s text; behaviour after it within the same line is unspecified.</div><div>R-LEX-2 When the input is standard input, zash reads no byte beyond the end of the command it is about to run. A command that reads standard input therefore sees the rest of the script. <em>Extension: POSIX allows a shell to read ahead.</em><br>Tests: <span style="display: inline-block">zash/stdin-script</span></div><div>R-LEX-3 A backslash immediately followed by newline, outside single quotes and outside <span style="display: inline-block">$'...'</span>, is removed together with the newline before tokenisation (line continuation). Inside a here-document body it is removed only when the delimiter was unquoted.</div><h3 id="loc-6">2.2 Tokens</h3><p>The lexer turns input into a sequence of tokens. A token is an <em>operator</em>, a <em>word</em>, an <span style="display: inline-block">IO_NUMBER</span> or <span style="display: inline-block">NEWLINE</span>.</p><div><p>R-LEX-4 The operators are exactly:</p><pre><code>control: & && ( ) ; ;; | || NEWLINE
redirection: < > >| >> <> <& >& << <<-</code></pre><p>The longest operator that matches the input is taken. There is no <span style="display: inline-block">;&</span>, <span style="display: inline-block">;;&</span>, <span style="display: inline-block">|&</span>, <span style="display: inline-block">&></span> or <span style="display: inline-block"><<<</span>.</p></div><div>R-LEX-5 Unquoted blanks (space and tab) separate tokens and are otherwise discarded. An unquoted <span style="display: inline-block">#</span> at the start of a token begins a comment that extends to, but does not include, the next newline.</div><div>R-LEX-6 A token consisting only of decimal digits, immediately followed by <span style="display: inline-block"><</span> or <span style="display: inline-block">></span> with no blank between, is an <span style="display: inline-block">IO_NUMBER</span>. Only the values 0 to 9 name descriptors a script may use; a larger <span style="display: inline-block">IO_NUMBER</span> is an error when the redirection is performed (see <span style="text-decoration: underline"><a href="#redirection">Section 10</a></span>).<br>Tests: <span style="display: inline-block">posix/redir</span>, <span style="display: inline-block">posix/fdmap</span></div><div>R-LEX-7 Any other token is a <span style="display: inline-block">WORD</span>. A <span style="display: inline-block">WORD</span> that is a valid <span style="display: inline-block">NAME</span> followed by <span style="display: inline-block">=</span> is an <em>assignment word</em> when it appears where <span style="text-decoration: underline"><a href="#grammar">Section 4</a></span> allows one. A <span style="display: inline-block">NAME</span> is a non-empty sequence of letters, digits and underscores that does not begin with a digit; letters are <span style="display: inline-block">a</span>–<span style="display: inline-block">z</span> and <span style="display: inline-block">A</span>–<span style="display: inline-block">Z</span> of the build’s character set.</div><h3 id="loc-7">2.3 Quoting</h3><p>Quoting determines which characters are literal. Each byte of a word carries a <em>quoted</em> flag that survives until field splitting and pathname expansion have used it (<span style="text-decoration: underline"><a href="#expansion">Section 5</a></span>); nothing is encoded in the text.</p><div>R-LEX-8 <strong>Backslash.</strong> Outside quotes, <span style="display: inline-block">\c</span> makes <span style="display: inline-block">c</span> literal and quoted, for any <span style="display: inline-block">c</span> but newline (line continuation, above).<br>Tests: <span style="display: inline-block">posix/quoting</span></div><div>R-LEX-9 <strong>Single quotes.</strong> <span style="display: inline-block">'...'</span> makes every byte up to the next <span style="display: inline-block">'</span> literal and quoted. A single quote cannot occur inside.<br>Tests: <span style="display: inline-block">posix/quoting</span></div><div>R-LEX-10 <strong>Double quotes.</strong> Inside <span style="display: inline-block">"..."</span> every byte is quoted. <span style="display: inline-block">$</span> and backquote keep their meaning. A backslash keeps its meaning only before <span style="display: inline-block">$</span>, backquote, <span style="display: inline-block">"</span>, <span style="display: inline-block">\</span> and newline; elsewhere it is literal. Inside <span style="display: inline-block">"${...}"</span> a further pair of double quotes nests: <span style="display: inline-block">"${x:-"}"}"</span> is one word whose default is the string <span style="display: inline-block">}</span>.<br>Tests: <span style="display: inline-block">posix/quoting</span>, <span style="display: inline-block">posix/param-word</span></div><div><p>R-LEX-11 <strong>Dollar-single quotes.</strong> <span style="display: inline-block">$'...'</span> is a quoted string in which these escapes are decoded when the word is read, and the result is literal and quoted:</p><table><tr><td><span style="display: inline-block">\a \b \e \f \n \r \t \v</span></td><td>the control character so named (<span style="display: inline-block">\e</span> is escape)</td></tr><tr><td><span style="display: inline-block">\\</span> <span style="display: inline-block">\'</span> <span style="display: inline-block">\"</span> <span style="display: inline-block">\?</span></td><td>the character itself</td></tr><tr><td><span style="display: inline-block">\</span><em>ooo</em></td><td>the byte with octal value <em>ooo</em> (one to three digits)</td></tr><tr><td><span style="display: inline-block">\x</span><em>hh</em></td><td>the byte with hexadecimal value <em>hh</em> (one or two digits)</td></tr><tr><td><span style="display: inline-block">\c</span><em>X</em></td><td>control-<em>X</em>, in ASCII builds only</td></tr><tr><td>any other escape</td><td>kept as written, backslash included</td></tr></table><p><span style="display: inline-block">$'...'</span> is recognised only where <span style="display: inline-block">$</span> would begin an expansion outside double quotes. Inside double quotes and in here-documents it is not special. <em>Extension: POSIX.1-2024 has <span style="display: inline-block">$'...'</span>; the set of escapes is zash’s.</em><br>Tests: <span style="display: inline-block">zash/ansi-quote.ascii</span></p></div><div><strong>Open.</strong> Does <span style="display: inline-block">\u</span> (and <span style="display: inline-block">\U</span>) in <span style="display: inline-block">$'...'</span> decode a code point? zash currently keeps it literally; bash, ksh93 and mksh encode UTF-8 and dash rejects it. Decide, then write the rule.</div><h3 id="reserved">2.4 Reserved words</h3><div><p>R-LEX-12 The reserved words are</p><pre><code>! { } case do done elif else esac fi for function if in
then time until while [[ ]]</code></pre><p>A word is recognised as reserved only when it is unquoted, is the whole token and appears where <span style="text-decoration: underline"><a href="#grammar">Section 4</a></span> expects that reserved word (in particular, as the first word of a command, or <span style="display: inline-block">in</span>/<span style="display: inline-block">do</span> in the positions of <span style="display: inline-block">for</span> and <span style="display: inline-block">case</span>). Elsewhere it is an ordinary word. <em>Difference: <span style="display: inline-block">function</span>, <span style="display: inline-block">time</span>, <span style="display: inline-block">[[</span> and <span style="display: inline-block">]]</span> are reserved in zash, so no command can be named <span style="display: inline-block">function</span>.</em><br>Tests: <span style="display: inline-block">posix/syntax</span>, <span style="display: inline-block">zash/function-kw</span>, <span style="display: inline-block">zash/cond</span></p></div><h3 id="loc-8">2.5 Here-documents</h3><div>R-LEX-13 For each <span style="display: inline-block"><<</span> or <span style="display: inline-block"><<-</span> operator, the word after it is the <em>delimiter</em>. The body is read starting at the line after the next <span style="display: inline-block">NEWLINE</span> token that is not itself inside a here-document, and consists of the lines up to (not including) a line equal to the delimiter after quote removal. Several here-documents on one line are read in order.<br>Tests: <span style="display: inline-block">posix/heredoc</span>, <span style="display: inline-block">posix/heredoc-edge</span></div><div>R-LEX-14 If any part of the delimiter was quoted, the body is literal. Otherwise the body is expanded as if in double quotes, except that <span style="display: inline-block">"</span> is not special: parameter, command and arithmetic expansion are performed and <span style="display: inline-block">\</span> is special only before <span style="display: inline-block">$</span>, backquote, <span style="display: inline-block">\</span> and newline.<br>Tests: <span style="display: inline-block">posix/heredoc</span>, <span style="display: inline-block">posix/heredoc-edge</span></div><div>R-LEX-15 With <span style="display: inline-block"><<-</span>, leading tab characters are removed from each body line and from the delimiter line before the comparison.<br>Tests: <span style="display: inline-block">posix/heredoc</span></div><div>R-LEX-16 A command substitution has its own here-document queue. A here-document started inside <span style="display: inline-block">$(...)</span> is read inside it, and one started outside is not satisfied by lines inside it. A <span style="display: inline-block">$(...)</span> that appears in a here-document body is parsed with the same rule.<br>Tests: <span style="display: inline-block">posix/heredoc-cmdsub</span>, <span style="display: inline-block">zash/heredoc-fuzz</span></div><div>R-LEX-17 End of input before the delimiter line ends the body without error.<br>Tests: <span style="display: inline-block">posix/heredoc</span></div><h2 id="aliases">3 Aliases</h2><div>R-ALI-1 When a <span style="display: inline-block">WORD</span> that is unquoted and a valid alias name occurs where the first word of a simple command is expected, and an alias of that name is defined, the word is replaced by the alias’s value, which is then read as input.<br>Tests: <span style="display: inline-block">zash/alias</span></div><div>R-ALI-2 If an alias’s value ends in a blank, the next word is also checked for alias substitution.<br>Tests: <span style="display: inline-block">zash/alias</span></div><div>R-ALI-3 <strong>Cycles.</strong> While substituting, each alias may be used at most once in one chain of substitutions: a chain starts at a command word and continues only through values that end in a blank or that consist of a single word that is itself an alias. An alias already used in the current chain is left as an ordinary word. Thus <span style="display: inline-block">alias a=b b=a; a</span> runs a command named <span style="display: inline-block">a</span> (or <span style="display: inline-block">b</span>) and does not loop.<br>Tests: <span style="display: inline-block">posix/alias-cycle</span></div><div>R-ALI-4 Alias substitution happens when a command is parsed, not when it runs. An alias defined by a command takes effect for commands parsed after that command has run, never for the rest of the line or compound command that defined it.</div><h2 id="grammar">4 Grammar</h2><h3 id="loc-9">4.1 Productions</h3><pre><code>program = linebreak [ complete_list ] ;
complete_list = list [ separator ] ;
list = and_or { separator_op linebreak and_or } ;
and_or = pipeline { ( '&&' | '||' ) linebreak pipeline } ;
pipeline = [ 'time' [ '-p' ] ] [ '!' ] pipe_sequence
| 'time' [ '-p' ] ; (* time with no command *)
pipe_sequence = command { '|' linebreak command } ;
command = simple_command
| compound_command { redirection }
| function_definition ;
compound_command
= brace_group | subshell | arith_command | cond_command
| for_clause | case_clause | if_clause
| while_clause | until_clause ;
brace_group = '{' compound_list '}' ;
subshell = '(' compound_list ')' ;
arith_command = '((' ARITH_TEXT '))' ; (* see the (( rule *)
cond_command = '[[' cond_expr ']]' ; (* see the [[ ]] chapter *)
for_clause = 'for' NAME linebreak
[ 'in' { WORD } sequential_sep ] do_group ;
case_clause = 'case' WORD linebreak 'in' linebreak
{ case_item } 'esac' ;
case_item = [ '(' ] pattern { '|' pattern } ')'
( compound_list | linebreak )
( ';;' linebreak | (* before esac *) ) ;
pattern = WORD ;
if_clause = 'if' compound_list 'then' compound_list
{ 'elif' compound_list 'then' compound_list }
[ 'else' compound_list ] 'fi' ;
while_clause = 'while' compound_list do_group ;
until_clause = 'until' compound_list do_group ;
do_group = 'do' compound_list 'done' ;
function_definition
= NAME '(' ')' linebreak function_body
| 'function' NAME linebreak function_body ;
function_body = compound_command { redirection } ;
simple_command = prefix [ WORD { WORD | redirection } ]
| WORD { WORD | redirection } ;
prefix = ( ASSIGNMENT_WORD | redirection )
{ ASSIGNMENT_WORD | redirection } ;
redirection = [ IO_NUMBER ] redir_op WORD ;
redir_op = '<' | '>' | '>|' | '>>' | '<>' | '<&' | '>&' | '<<' | '<<-' ;
compound_list = linebreak list [ separator ] ;
separator_op = '&' | ';' ;
separator = separator_op linebreak | newline_list ;
sequential_sep = ';' linebreak | newline_list ;
linebreak = [ newline_list ] ;
newline_list = NEWLINE { NEWLINE } ;</code></pre><h3 id="loc-10">4.2 Rules attached to the grammar</h3><div>R-GRM-1 The grammar above is that of POSIX XCU 2.10 with these changes: <span style="display: inline-block">time</span> and <span style="display: inline-block">!</span> prefixes on a pipeline, the <span style="display: inline-block">function</span> form of a function definition, <span style="display: inline-block">arith_command</span> and <span style="display: inline-block">cond_command</span>. A program that the grammar does not derive is a syntax error (<span style="text-decoration: underline"><a href="#errors">Section 12</a></span>).<br>Tests: <span style="display: inline-block">posix/syntax</span></div><div>R-GRM-2 An <span style="display: inline-block">ASSIGNMENT_WORD</span> is recognised only in <span style="display: inline-block">prefix</span>, that is, before the command name. After the command name, <span style="display: inline-block">NAME=value</span> is an ordinary argument.<br>Tests: <span style="display: inline-block">posix/syntax</span></div><div>R-GRM-3 <span style="display: inline-block">function NAME body</span> and <span style="display: inline-block">NAME() body</span> define the same thing: a function whose body is the compound command with its redirections. The two forms are identical. <em>Difference: ksh gives the <span style="display: inline-block">function</span> form local traps and <span style="display: inline-block">$0</span>; zash does not.</em><br>Tests: <span style="display: inline-block">zash/function-kw</span></div><div>R-GRM-4 In a <span style="display: inline-block">case_clause</span>, the last <span style="display: inline-block">case_item</span> may omit <span style="display: inline-block">;;</span> before <span style="display: inline-block">esac</span>. There is no fall-through operator.<br>Tests: <span style="display: inline-block">posix/case-edge</span></div><div>R-GRM-5 <span style="display: inline-block">for NAME do ...</span> and <span style="display: inline-block">for NAME; do ...</span> without <span style="display: inline-block">in</span> iterate over the positional parameters as they were when the loop started (<span style="text-decoration: underline"><a href="#commands">Section 8</a></span>).</div><div><p>R-GRM-6 <strong>Choosing between <span style="display: inline-block">((</span> and <span style="display: inline-block">( (</span>.</strong> When <span style="display: inline-block">((</span> begins a command, the text up to the matching <span style="display: inline-block">))</span> is an <span style="display: inline-block">arith_command</span> unless one of the following holds, in which case the input is read as a subshell whose first command is itself a subshell:</p><ol><li>a <span style="display: inline-block">;</span> occurs outside quotes before the matching <span style="display: inline-block">))</span>;</li><li>a word character (letter, digit, <span style="display: inline-block">_</span>, <span style="display: inline-block">$</span>, <span style="display: inline-block">"</span>, <span style="display: inline-block">'</span>) follows a word that ended in a letter, digit, <span style="display: inline-block">_</span> or <span style="display: inline-block">}</span>, with only blanks or newlines between them (two adjacent words: a command, not an expression);</li><li>the text exceeds 4096 bytes;</li><li>the text begins with more than 8 consecutive <span style="display: inline-block">(</span>;</li><li>a <span style="display: inline-block">)</span> that closes the outermost level is not immediately followed by a second <span style="display: inline-block">)</span>.</li></ol><p>The scan for the matching <span style="display: inline-block">))</span> honours quotes and backslashes. To be certain of nested subshells, write <span style="display: inline-block">( (</span>. <em>Difference: a heuristic; POSIX leaves <span style="display: inline-block">((</span> unspecified.</em><br>Tests: <span style="display: inline-block">zash/arith-ext</span>, <span style="display: inline-block">posix/subshell-edge</span></p></div><div><strong>Note.</strong> The parser decides this with a bounded scan of the characters. A formalisation should state the rule as a decidable predicate on the character sequence and prove that the parser applies it.</div><div>R-GRM-7 <span style="display: inline-block">$((</span> always begins arithmetic expansion. Write <span style="display: inline-block">$( (</span> for a command substitution that begins with a subshell.<br>Tests: <span style="display: inline-block">posix/cmdsub</span></div><h2 id="expansion">5 Word expansion</h2><h3 id="loc-11">5.1 Words and fields</h3><p>A word, as parsed, is a list of <em>parts</em>. A part is literal text, a parameter expansion, a command substitution or an arithmetic expansion, and each part carries a <em>quoted</em> flag. Expansion turns a word into a list of <em>fields</em>; each byte of a field carries a quoted flag inherited from the part that produced it.</p><p>Formally, let <math><mi>𝐵</mi></math> be the set of bytes and <math><mi>𝑄</mi><mo>=</mo><mi>𝐵</mi><mo lspace="0.2222222222222222em" rspace="0.2222222222222222em">×</mo><mrow><mo>{</mo><mrow><mn>0</mn><mo>,</mo><mn>1</mn></mrow><mo>}</mo></mrow></math> the set of flagged bytes. A field is a finite sequence in <math><msup><mi>𝑄</mi><mo>∗</mo></msup></math>, and</p><math display="block"><mrow><mo>⟦</mo><mi>𝑤</mi><mo>⟧</mo></mrow><mo lspace="0.2777777777777778em" rspace="0.2777777777777778em">:</mo><mtext>Word</mtext><mo stretchy="false">→</mo><msup><mrow><mo>(</mo><mtext>Field</mtext><mo>)</mo></mrow><mo>∗</mo></msup></math><p>is computed in the four steps below. Quote removal is the projection <math><msub><mi>𝜋</mi><mn>1</mn></msub></math> from <math><msup><mi>𝑄</mi><mo>∗</mo></msup></math> to <math><msup><mi>𝐵</mi><mo>∗</mo></msup></math> applied to every resulting field.</p><div><p>R-EXP-1 <strong>Order.</strong> For each word, the shell performs</p><ol><li>tilde expansion, parameter expansion, command substitution and arithmetic expansion, in one left-to-right pass over the parts;</li><li>field splitting of the results of unquoted expansions (step 1 only);</li><li>pathname expansion of each field that contains an unquoted pattern character;</li><li>quote removal.</li></ol><p>No byte that came from the text of the script is ever split, and no quoted byte is ever split or treated as a pattern character.<br>Tests: <span style="display: inline-block">posix/expand-edge</span>, <span style="display: inline-block">posix/edge</span></p></div><div>R-EXP-2 <strong>Contexts without splitting.</strong> In assignments (<span style="display: inline-block">NAME=word</span>), the word of a <span style="display: inline-block">case</span>, the target of a redirection other than a here-document, the operands of <span style="display: inline-block">[[ ]]</span> and the word in <span style="display: inline-block">${x op word}</span>, steps 2 and 3 are not performed and the result is one field. (A redirection target that would expand to more than one field is still one field.)<br>Tests: <span style="display: inline-block">posix/expand-edge</span></div><h3 id="loc-12">5.2 Tilde expansion</h3><div>R-EXP-3 An unquoted <span style="display: inline-block">~</span> at the start of a word, followed by the characters up to the first unquoted <span style="display: inline-block">/</span> (or the end of the word), is a tilde-prefix. <span style="display: inline-block">~</span> alone is replaced by <span style="display: inline-block">$HOME</span>; <span style="display: inline-block">~name</span> by the home directory of user <em>name</em>. If the user does not exist the prefix is left unchanged. The result is quoted (it is not split or globbed).<br>Tests: <span style="display: inline-block">posix/misc</span>, <span style="display: inline-block">posix/param-word</span></div><div>R-EXP-4 In an assignment word, tilde expansion is also performed after each unquoted <span style="display: inline-block">:</span> in the value.<br>Tests: <span style="display: inline-block">posix/misc</span></div><div>R-EXP-5 The word in <span style="display: inline-block">${x-word}</span>, <span style="display: inline-block">${x=word}</span>, <span style="display: inline-block">${x?word}</span> and <span style="display: inline-block">${x+word}</span> (and their <span style="display: inline-block">:</span> forms) undergoes tilde expansion when it is not within double quotes.<br>Tests: <span style="display: inline-block">posix/param-word</span></div><h3 id="loc-13">5.3 Parameter expansion</h3><div>R-EXP-6 A parameter is a <span style="display: inline-block">NAME</span> (a variable), a positional parameter (a decimal number; more than one digit requires braces: <span style="display: inline-block">${10}</span>), or one of the special parameters <span style="display: inline-block">@ * # ? - $ !</span> and <span style="display: inline-block">0</span>.<br>Tests: <span style="display: inline-block">posix/params</span></div><div><p>R-EXP-7 The conditional forms, for a parameter <math><mi>𝑥</mi></math> and a word <math><mi>𝑤</mi></math>, yield:</p><table><tr><td><strong>form</strong></td><td><strong><math><mi>𝑥</mi></math> set, non-empty</strong></td><td><strong><math><mi>𝑥</mi></math> set, empty</strong></td><td><strong><math><mi>𝑥</mi></math> unset</strong></td></tr><tr><td><span style="display: inline-block">${x:-w}</span></td><td><math><mi>𝑥</mi></math></td><td><math><mrow><mo>⟦</mo><mi>𝑤</mi><mo>⟧</mo></mrow></math></td><td><math><mrow><mo>⟦</mo><mi>𝑤</mi><mo>⟧</mo></mrow></math></td></tr><tr><td><span style="display: inline-block">${x-w}</span></td><td><math><mi>𝑥</mi></math></td><td>empty</td><td><math><mrow><mo>⟦</mo><mi>𝑤</mi><mo>⟧</mo></mrow></math></td></tr><tr><td><span style="display: inline-block">${x:=w}</span></td><td><math><mi>𝑥</mi></math></td><td>assign <math><mrow><mo>⟦</mo><mi>𝑤</mi><mo>⟧</mo></mrow></math></td><td>assign <math><mrow><mo>⟦</mo><mi>𝑤</mi><mo>⟧</mo></mrow></math></td></tr><tr><td><span style="display: inline-block">${x=w}</span></td><td><math><mi>𝑥</mi></math></td><td>empty</td><td>assign <math><mrow><mo>⟦</mo><mi>𝑤</mi><mo>⟧</mo></mrow></math></td></tr><tr><td><span style="display: inline-block">${x:?w}</span></td><td><math><mi>𝑥</mi></math></td><td>error</td><td>error</td></tr><tr><td><span style="display: inline-block">${x?w}</span></td><td><math><mi>𝑥</mi></math></td><td>empty</td><td>error</td></tr><tr><td><span style="display: inline-block">${x:+w}</span></td><td><math><mrow><mo>⟦</mo><mi>𝑤</mi><mo>⟧</mo></mrow></math></td><td>empty</td><td>empty</td></tr><tr><td><span style="display: inline-block">${x+w}</span></td><td><math><mrow><mo>⟦</mo><mi>𝑤</mi><mo>⟧</mo></mrow></math></td><td><math><mrow><mo>⟦</mo><mi>𝑤</mi><mo>⟧</mo></mrow></math></td><td>empty</td></tr></table><p><em>assign</em> sets <math><mi>𝑥</mi></math> to the value and yields it; a positional or special parameter cannot be assigned to this way; attempting to do so is an error. <em>error</em> writes <math><mrow><mo>⟦</mo><mi>𝑤</mi><mo>⟧</mo></mrow></math>, or a default message if <math><mi>𝑤</mi></math> is empty, and is an expansion error (<span style="text-decoration: underline"><a href="#errors">Section 12</a></span>). The word <math><mi>𝑤</mi></math> is expanded only when its value is used.<br>Tests: <span style="display: inline-block">posix/params</span>, <span style="display: inline-block">posix/expand-edge</span></p></div><div>R-EXP-8 <span style="display: inline-block">${#x}</span> is the length of the value of <math><mi>𝑥</mi></math> in bytes, as a decimal number. <span style="display: inline-block">${#*}</span> and <span style="display: inline-block">${#@}</span> are the number of positional parameters.<br>Tests: <span style="display: inline-block">posix/params</span></div><div><p>R-EXP-9 <strong>Trimming.</strong> For a pattern <math><mi>𝑝</mi></math> (the word, expanded, with its quoted bytes literal):</p><ul><li><span style="display: inline-block">${x#p}</span> removes the shortest prefix of <math><mi>𝑥</mi></math> that <math><mi>𝑝</mi></math> matches;</li><li><span style="display: inline-block">${x##p}</span> removes the longest such prefix;</li><li><span style="display: inline-block">${x%p}</span> removes the shortest matching suffix;</li><li><span style="display: inline-block">${x%%p}</span> removes the longest matching suffix.</li></ul><p>If no prefix (suffix) matches, the value is unchanged. Matching is that of <span style="text-decoration: underline"><a href="#patterns">Section 6</a></span>, applied to the whole candidate prefix or suffix.<br>Tests: <span style="display: inline-block">posix/params</span>, <span style="display: inline-block">zash/trim-long</span></p></div><div><strong>Note.</strong> zash computes a trim in one pass for patterns of up to 63 elements and tries each length otherwise. Both methods must give the shortest or longest match this rule defines. That proof is the first theorem planned for the Lean model.</div><div>R-EXP-10 With <span style="display: inline-block">set -u</span>, expanding an unset parameter other than <span style="display: inline-block">@</span> and <span style="display: inline-block">*</span>, except in a conditional form that handles unset, is an expansion error.<br>Tests: <span style="display: inline-block">zash/ash-differences</span></div><div>R-EXP-11 <strong><span style="display: inline-block">$@</span> and <span style="display: inline-block">$*</span>.</strong> Unquoted, each positional parameter becomes a separate field before field splitting, and each is then split. Quoted, <span style="display: inline-block">"$@"</span> yields one field per positional parameter, with any adjacent text of the word joined to the first and last of them, and no field at all when there are none; <span style="display: inline-block">"$*"</span> yields one field: the parameters joined by the first character of <span style="display: inline-block">IFS</span> (by nothing if <span style="display: inline-block">IFS</span> is empty, by a space if <span style="display: inline-block">IFS</span> is unset).<br>Tests: <span style="display: inline-block">posix/expand-edge</span>, <span style="display: inline-block">posix/split</span></div><div><strong>Open.</strong> Unquoted <span style="display: inline-block">$@</span> and <span style="display: inline-block">$*</span> in a here-document body and in other no-splitting contexts. Differential testing showed zash treating quote characters there differently from dash, bash and ksh93. Decide which behaviour is intended, then write the rule.</div><h3 id="loc-14">5.4 Command substitution</h3><div>R-EXP-12 <span style="display: inline-block">$(list)</span>, and the older backquoted form, run <em>list</em> in a subshell environment and are replaced by its standard output with every trailing newline removed. <span style="display: inline-block">$(...)</span> is parsed when the enclosing word is read; the backquoted form is parsed after backquote backslash processing.<br>Tests: <span style="display: inline-block">posix/cmdsub</span></div><div>R-EXP-13 <span style="display: inline-block">$(<file)</span> is replaced by the contents of <em>file</em>, with trailing newlines removed, as if by <span style="display: inline-block">$(cat file)</span>. A file that cannot be opened is an expansion error.<br>Tests: <span style="display: inline-block">zash/purebuiltin</span>, <span style="display: inline-block">zash/cmdsub-file</span></div><div><strong>Note.</strong> Whether a substitution creates a process is not part of the language; zash runs many of them in the shell. The observable behaviour must be that of a subshell: assignments and <span style="display: inline-block">cd</span> inside do not affect the caller. The process-accounting tests (<span style="display: inline-block">tests/proc/</span>) check the cost, and are not cited by rules.</div><h3 id="loc-15">5.5 Arithmetic expansion</h3><div>R-EXP-14 <span style="display: inline-block">$((expression))</span> is replaced by the decimal value of the expression (<span style="text-decoration: underline"><a href="#arithmetic">Section 7</a></span>). The text of the expression first undergoes parameter expansion, command substitution and quote removal, as in double quotes.<br>Tests: <span style="display: inline-block">posix/arith</span></div><h3 id="loc-16">5.6 Field splitting</h3><p>Let <math><mi>𝐼</mi></math> be the value of <span style="display: inline-block">IFS</span> (if unset, space, tab and newline). Bytes of <math><mi>𝐼</mi></math> that are space, tab or newline are <em>IFS white space</em>; the others are <em>IFS delimiters</em>.</p><div><p>R-EXP-15 Only unquoted bytes that came from an unquoted expansion are considered. If <math><mi>𝐼</mi></math> is empty, no splitting is done. Otherwise:</p><ol><li>IFS white space at the start and end of the expansion result is ignored;</li><li>each IFS delimiter, with any IFS white space next to it, ends a field;</li><li>each other sequence of IFS white space ends a field;</li><li>a field is produced between two delimiters even if it is empty, but no empty field is produced at the end.</li></ol><p><br>Tests: <span style="display: inline-block">posix/split</span></p></div><div>R-EXP-16 A word whose expansion produces no bytes at all and contains no quoted part produces no field (it disappears). A word with a quoted part that is empty (<span style="display: inline-block">""</span>) produces one empty field.<br>Tests: <span style="display: inline-block">posix/split</span></div><h3 id="loc-17">5.7 Pathname expansion</h3><div>R-EXP-17 A field containing an unquoted <span style="display: inline-block">*</span>, <span style="display: inline-block">?</span> or <span style="display: inline-block">[</span> is a pattern (<span style="text-decoration: underline"><a href="#patterns">Section 6</a></span>) matched against pathnames. <span style="display: inline-block">/</span> is never matched by a pattern character; each slash-separated component is matched separately. A component beginning with <span style="display: inline-block">.</span> is matched only by a pattern component beginning with an unquoted <span style="display: inline-block">.</span>. The matches replace the field, in the collating order of <span style="text-decoration: underline"><a href="#patterns">Section 6</a></span>. If nothing matches, the field is left as it is.<br>Tests: <span style="display: inline-block">posix/glob</span>, <span style="display: inline-block">posix/glob-edge</span></div><div>R-EXP-18 With <span style="display: inline-block">set -f</span>, pathname expansion is not performed.<br>Tests: <span style="display: inline-block">posix/glob</span></div><h2 id="patterns">6 Patterns</h2><div><p>R-PAT-1 A pattern is a sequence of elements. Each unquoted byte is:</p><ul><li><span style="display: inline-block">*</span>: matches any sequence of bytes, including the empty one;</li><li><span style="display: inline-block">?</span>: matches any one byte;</li><li><span style="display: inline-block">[</span>: begins a bracket expression (below) if a matching <span style="display: inline-block">]</span> follows; otherwise it is literal;</li><li>anything else: matches itself.</li></ul><p>A quoted byte always matches only itself.<br>Tests: <span style="display: inline-block">posix/case-edge</span>, <span style="display: inline-block">posix/glob-edge</span></p></div><div>R-PAT-2 A bracket expression <span style="display: inline-block">[...]</span> matches one byte that is in its set. If its first byte is <span style="display: inline-block">!</span> or <span style="display: inline-block">^</span>, it matches one byte that is not in the set. A <span style="display: inline-block">]</span> in the first position (immediately after any negation) is a literal member. <span style="display: inline-block">a-b</span> is a range: all bytes whose code is between those of <math><mi>𝑎</mi></math> and <math><mi>𝑏</mi></math> inclusive, <em>in the build’s character set</em>. <span style="display: inline-block">[:class:]</span> adds the bytes of a character class: <mark>alnum alpha blank cntrl digit graph lower print punct space upper xdigit</mark>. <em>Difference: <span style="display: inline-block">^</span> as negation is not POSIX.</em><br>Tests: <span style="display: inline-block">posix/case-edge</span>, <span style="display: inline-block">posix/charset</span></div><div>R-PAT-3 Ranges and the collating order of pathname expansion use byte codes, not the locale. In an EBCDIC build <span style="display: inline-block">[a-z]</span> therefore contains more than the 26 lower-case letters; scripts that must be portable use <span style="display: inline-block">[[:lower:]]</span>.<br>Tests: <span style="display: inline-block">posix/collate.ascii</span></div><div><p>R-PAT-4 For a pattern <math><mi>𝑝</mi></math> and a string <math><mi>𝑠</mi></math>, <math><mtext>match</mtext><mrow><mo>(</mo><mrow><mi>𝑝</mi><mo>,</mo><mi>𝑠</mi></mrow><mo>)</mo></mrow></math> holds when <math><mi>𝑝</mi></math> matches the whole of <math><mi>𝑠</mi></math>. It is defined by:</p><math display="block"><mtable class="multiline-equation aligned"><mtr><mtd><mtext>match</mtext><mrow><mo>(</mo><mrow><mi>𝜀</mi><mo>,</mo><mi>𝑠</mi></mrow><mo>)</mo></mrow></mtd><mtd><mo form="infix" stretchy="false">⟺</mo><mi>𝑠</mi><mo>=</mo><mi>𝜀</mi></mtd></mtr><mtr><mtd><mtext>match</mtext><mrow><mo>(</mo><mrow><mo form="infix" lspace="0em" rspace="0.2222222222222222em">∗</mo><msup><mi>𝑝</mi><mo>′</mo></msup><mo>,</mo><mi>𝑠</mi></mrow><mo>)</mo></mrow></mtd><mtd><mo form="infix" stretchy="false">⟺</mo><mo form="prefix">∃</mo><mi>𝑖</mi><mi>.</mi><mspace width="0.2222em"/><mtext>match</mtext><mrow><mo>(</mo><mrow><msup><mi>𝑝</mi><mo>′</mo></msup><mo>,</mo><mi>𝑠</mi><mrow><mo>[</mo><mrow><mi>𝑖</mi><mi>.</mi><mi>.</mi></mrow><mo>]</mo></mrow></mrow><mo>)</mo></mrow></mtd></mtr><mtr><mtd><mtext>match</mtext><mrow><mo>(</mo><mrow><mi>𝑒</mi><msup><mi>𝑝</mi><mo>′</mo></msup><mo>,</mo><mi>𝑐</mi><msup><mi>𝑠</mi><mo>′</mo></msup></mrow><mo>)</mo></mrow></mtd><mtd><mo form="infix" stretchy="false">⟺</mo><mi>𝑒</mi><mspace width="0.2222em"/><mtext>matches</mtext><mspace width="0.2222em"/><mi>𝑐</mi><mo>∧</mo><mtext>match</mtext><mrow><mo>(</mo><mrow><msup><mi>𝑝</mi><mo>′</mo></msup><mo>,</mo><msup><mi>𝑠</mi><mo>′</mo></msup></mrow><mo>)</mo></mrow></mtd></mtr></mtable></math><p>where <math><mi>𝑒</mi></math> is <span style="display: inline-block">?</span>, a bracket expression or a literal byte.</p></div><h2 id="arithmetic">7 Arithmetic</h2><h3 id="loc-18">7.1 Syntax</h3><pre><code>expr = assign { ',' assign } ;
assign = cond | LVALUE assign_op assign ;
assign_op= '=' | '*=' | '/=' | '%=' | '+=' | '-=' | '<<=' | '>>='
| '&=' | '^=' | '|=' ;
cond = lor [ '?' expr ':' cond ] ;
lor = land { '||' land } ;
land = bor { '&&' bor } ;
bor = bxor { '|' bxor } ;
bxor = band { '^' band } ;
band = equ { '&' equ } ;
equ = rel { ( '==' | '!=' ) rel } ;
rel = shift { ( '<' | '<=' | '>' | '>=' ) shift } ;
shift = add { ( '<<' | '>>' ) add } ;
add = mul { ( '+' | '-' ) mul } ;
mul = unary { ( '*' | '/' | '%' ) unary } ;
unary = ( '+' | '-' | '!' | '~' ) unary
| ( '++' | '--' ) LVALUE
| postfix ;
postfix = primary | LVALUE ( '++' | '--' ) ;
primary = NUMBER | LVALUE | '(' expr ')' ;
LVALUE = NAME ;
NUMBER = decimal | '0' octal_digits | ( '0x' | '0X' ) hex_digits ;</code></pre><p>Blanks and newlines between tokens are ignored.</p><h3 id="loc-19">7.2 Values</h3><div><p>R-ARI-1 Values are 64-bit two’s-complement integers. Every operation is computed modulo <math><msup><mn>2</mn><mn>64</mn></msup></math> and the result is taken as signed: overflow wraps and is not an error. In particular</p><math display="block"><mrow><mo>⟦</mo><mrow><mi>𝑎</mi><mo>+</mo><mi>𝑏</mi></mrow><mo>⟧</mo></mrow><mo>=</mo><mtext>signed</mtext><mrow><mo>(</mo><mrow><mrow><mo>(</mo><mrow><mrow><mo>⟦</mo><mi>𝑎</mi><mo>⟧</mo></mrow><mo>+</mo><mrow><mo>⟦</mo><mi>𝑏</mi><mo>⟧</mo></mrow></mrow><mo>)</mo></mrow><mo form="prefix" lspace="0.16666666666666666em" rspace="0.16666666666666666em">mod</mo><msup><mn>2</mn><mn>64</mn></msup></mrow><mo>)</mo></mrow></math><p>and likewise for <span style="display: inline-block">-</span>, <span style="display: inline-block">*</span>, unary <span style="display: inline-block">-</span> and <span style="display: inline-block"><<</span>.<br>Tests: <span style="display: inline-block">posix/arith</span>, <span style="display: inline-block">posix/arith-edge</span></p></div><div>R-ARI-2 <span style="display: inline-block">/</span> truncates toward zero and <span style="display: inline-block">%</span> has the sign of the dividend. A right operand of 0 for <span style="display: inline-block">/</span> or <span style="display: inline-block">%</span> is an error. The quotient of the most negative value by <math><mo>−</mo><mn>1</mn></math> is the most negative value (it wraps).<br>Tests: <span style="display: inline-block">posix/arith-edge</span></div><div>R-ARI-3 Shift counts are taken modulo 64. <span style="display: inline-block">>></span> is an arithmetic shift.<br>Tests: <span style="display: inline-block">posix/arith-edge</span></div><div>R-ARI-4 Relational, equality and logical operators yield 1 for true and 0 for false. <span style="display: inline-block">&&</span>, <span style="display: inline-block">||</span> and <span style="display: inline-block">?:</span> evaluate their right (or untaken) operand only when needed; an assignment in an operand that is not evaluated does not happen.<br>Tests: <span style="display: inline-block">posix/arith</span></div><div>R-ARI-5 <strong>Variables.</strong> A <span style="display: inline-block">NAME</span> in an expression stands for the value of that variable: 0 if it is unset or empty; otherwise the value is itself evaluated as an arithmetic expression. Such nested evaluation may go 16 levels deep; exceeding this depth is an error (“expression recursion too deep”).<br>Tests: <span style="display: inline-block">zash/arith-ext</span>, <span style="display: inline-block">zash/ash-differences</span></div><div>R-ARI-6 <span style="display: inline-block">++</span>, <span style="display: inline-block">--</span>, the compound assignments and <span style="display: inline-block">,</span> are as in C. An assignment stores the decimal value in the variable and yields it. Assigning to a read-only variable is an error. <em>Extension: POSIX requires neither <span style="display: inline-block">++</span>/<span style="display: inline-block">--</span> nor <span style="display: inline-block">,</span>.</em><br>Tests: <span style="display: inline-block">zash/arith-ext</span></div><div>R-ARI-7 A <span style="display: inline-block">NUMBER</span> that does not fit in 64 bits, or text that the syntax does not derive, is an error.<br>Tests: <span style="display: inline-block">posix/arith-edge</span></div><h2 id="commands">8 Commands</h2><h3 id="loc-20">8.1 Exit status</h3><div>R-CMD-1 Every command has an exit status, an integer from 0 to 255. Zero is <em>true</em> (success), anything else <em>false</em>. <span style="display: inline-block">$?</span> is the status of the most recently completed pipeline.</div><div>R-CMD-2 For a simple command the status is the program’s exit status, except: 127 if the command was not found; 126 if it was found but could not be run; <math><mn>128</mn><mo>+</mo><mi>𝑛</mi></math> if it was ended by signal <math><mi>𝑛</mi></math>; 2 if a redirection on it failed; the status of the last command substitution if the command had no command name; 0 if it had neither.<br>Tests: <span style="display: inline-block">posix/execfail</span></div><h3 id="loc-21">8.2 Simple commands</h3><div><p>R-CMD-3 A simple command is performed in this order:</p><ol><li>the words that are not assignments or redirections are expanded (<span style="text-decoration: underline"><a href="#expansion">Section 5</a></span>); the first field, if any, is the command name;</li><li>redirections are performed, left to right (<span style="text-decoration: underline"><a href="#redirection">Section 10</a></span>);</li><li>each assignment is expanded and applied, left to right.</li></ol><p>If there is no command name, the assignments affect the current shell and redirections are performed and undone.<br>Tests: <span style="display: inline-block">posix/vars</span>, <span style="display: inline-block">posix/redir</span></p></div><div>R-CMD-4 <strong>Command search.</strong> A command name without <span style="display: inline-block">/</span> is looked up in this order: special builtin; function; regular builtin; then the directories of <span style="display: inline-block">PATH</span>, left to right, selecting the first executable file found. An empty <span style="display: inline-block">PATH</span> entry means the current directory. A name containing <span style="display: inline-block">/</span> is run as that path. A path found in <span style="display: inline-block">PATH</span> is remembered; it is looked up again if running it fails with “no such file” or “not a directory”.<br>Tests: <span style="display: inline-block">posix/functions</span>, <span style="display: inline-block">posix/misc-builtins</span></div><div>R-CMD-5 If the system refuses to execute a file because it is not an executable format, and the file begins with <span style="display: inline-block">#!</span>, the interpreter named on that line is run with the file as its argument; otherwise zash runs the file as a script in a new zash process, with the same arguments.<br>Tests: <span style="display: inline-block">posix/execfail</span>, <span style="display: inline-block">scripts/library</span></div><div>R-CMD-6 <strong>Assignments before a command.</strong> For an external command they are put in that command’s environment only. For a special builtin they affect the current shell and persist. For a function or a regular builtin they are in effect while it runs and are then undone.<br>Tests: <span style="display: inline-block">posix/vars</span>, <span style="display: inline-block">posix/functions</span></div><div>R-CMD-7 The special builtins are <mark>: . break continue eval exec exit export readonly return set shift times trap unset</mark>. An error in a special builtin is a fatal error in a non-interactive shell (<span style="text-decoration: underline"><a href="#errors">Section 12</a></span>), except when the builtin was run through <span style="display: inline-block">command</span>, in which case the error is only the command’s status.<br>Tests: <span style="display: inline-block">posix/command-special</span></div><h3 id="loc-22">8.3 Pipelines</h3><div>R-CMD-8 In <span style="display: inline-block">c1 | c2 | ... | cn</span>, the standard output of each command is connected to the standard input of the next. The commands run concurrently. The status is that of <span style="display: inline-block">cn</span>, unless <span style="display: inline-block">set -o pipefail</span> is on, in which case it is that of the last command that failed, or 0.<br>Tests: <span style="display: inline-block">posix/pipes</span></div><div>R-CMD-9 <strong>The last stage.</strong> When job control is off, <span style="display: inline-block">cn</span> runs in the current shell environment if it is not an external command: assignments and <span style="display: inline-block">cd</span> in it persist after the pipeline. Every other stage runs in a subshell environment. When job control is on (<span style="display: inline-block">set -m</span>), every stage runs in a subshell environment. <em>Difference: dash and bash run every stage in a subshell; this is the ksh and z/OS <span style="display: inline-block">/bin/sh</span> behaviour.</em><br>Tests: <span style="display: inline-block">zash/lastpipe</span>, <span style="display: inline-block">posix/pipes</span></div><div>R-CMD-10 <span style="display: inline-block">! pipeline</span> has status 0 if the pipeline’s status is not 0, and 1 otherwise.<br>Tests: <span style="display: inline-block">posix/control</span></div><div>R-CMD-11 <span style="display: inline-block">time pipeline</span> runs the pipeline and then writes to standard error the real time and the user and system CPU time it used, shell and children. <span style="display: inline-block">-p</span> selects the POSIX format. <span style="display: inline-block">time</span> alone reports the shell’s own totals. The status is the pipeline’s.<br>Tests: <span style="display: inline-block">zash/zos-options</span></div><h3 id="loc-23">8.4 Lists</h3><div>R-CMD-12 <span style="display: inline-block">a && b</span> runs <span style="display: inline-block">b</span> only if <span style="display: inline-block">a</span> succeeded; <span style="display: inline-block">a || b</span> only if it failed. The status is that of the last command run. <span style="display: inline-block">;</span> and newline run commands in sequence.<br>Tests: <span style="display: inline-block">posix/control</span></div><div>R-CMD-13 <span style="display: inline-block">c &</span> runs <span style="display: inline-block">c</span> asynchronously in a subshell environment, with standard input from <span style="display: inline-block">/dev/null</span> when job control is off and no input redirection is given. <span style="display: inline-block">$!</span> is set to the process ID of <span style="display: inline-block">c</span> (of its last stage, for a pipeline). The status of <span style="display: inline-block">c &</span> itself is 0.<br>Tests: <span style="display: inline-block">zash/jobs</span></div><h3 id="loc-24">8.5 Compound commands</h3><div>R-CMD-14 <span style="display: inline-block">( list )</span> runs <em>list</em> in a subshell environment: nothing it changes (variables, functions, options, directory, traps, open files) is visible afterwards. <span style="display: inline-block">{ list; }</span> runs <em>list</em> in the current environment. The status of each is that of <em>list</em>.<br>Tests: <span style="display: inline-block">posix/subshell-edge</span></div><div>R-CMD-15 <mark>for x in w1 ... wn; do body; done</mark> expands the words (with field splitting and pathname expansion) once, then for each resulting field assigns it to <em>x</em> and runs <em>body</em>. Without <span style="display: inline-block">in</span>, the fields are the positional parameters as they were when the loop started; changing them inside the body (for example with <span style="display: inline-block">set --</span>) does not change the iteration. The status is that of the last <em>body</em> run, or 0 if none ran.<br>Tests: <span style="display: inline-block">posix/control</span>, <span style="display: inline-block">posix/for-set</span></div><div>R-CMD-16 <span style="display: inline-block">case w in ...</span> expands <em>w</em> once (without splitting or globbing) and runs the body of the first item with a matching pattern (expanded likewise; <span style="text-decoration: underline"><a href="#patterns">Section 6</a></span>). Status: that of the body run, or 0 if none matched.<br>Tests: <span style="display: inline-block">posix/case-edge</span></div><div>R-CMD-17 <span style="display: inline-block">if</span>, <span style="display: inline-block">while</span> and <span style="display: inline-block">until</span> are as in POSIX. The status of a loop is that of the last body run, or 0 if none ran.<br>Tests: <span style="display: inline-block">posix/control</span></div><div>R-CMD-18 <span style="display: inline-block">break n</span> and <span style="display: inline-block">continue n</span> (default 1) leave, or go to the next iteration of, the <em>n</em>-th enclosing loop. An <em>n</em> larger than the depth refers to the outermost loop.<br>Tests: <span style="display: inline-block">posix/control</span></div><div>R-CMD-19 <span style="display: inline-block">(( expr ))</span> evaluates <em>expr</em> (<span style="text-decoration: underline"><a href="#arithmetic">Section 7</a></span>). Its status is 0 if the value is not zero and 1 if it is zero; 2 on an error.<br>Tests: <span style="display: inline-block">zash/arith-ext</span></div><h3 id="loc-25">8.6 Functions</h3><div>R-CMD-20 Defining a function stores its body; running the definition has status 0. Calling it runs the body with the positional parameters set to the call’s arguments, and restores them afterwards. <span style="display: inline-block">$0</span> is unchanged.<br>Tests: <span style="display: inline-block">posix/functions</span>, <span style="display: inline-block">posix/func-edge</span></div><div>R-CMD-21 <span style="display: inline-block">return n</span> ends the function with status <em>n</em> modulo 256 (so <span style="display: inline-block">return 300</span> gives 44); <span style="display: inline-block">return</span> alone uses <span style="display: inline-block">$?</span>.<br>Tests: <span style="display: inline-block">posix/func-edge</span>, <span style="display: inline-block">zash/ash-differences</span></div><div>R-CMD-22 <span style="display: inline-block">local NAME</span> and <span style="display: inline-block">typeset NAME</span> inside a function make <em>NAME</em> local to that call: its previous value and attributes are restored when the function returns. Callees see the local value (dynamic scope).<br>Tests: <span style="display: inline-block">zash/local</span>, <span style="display: inline-block">zash/typeset</span></div><h2 id="conditional">9 Conditional expressions <span style="display: inline-block">[[ ]]</span></h2><pre><code>cond_expr = cond_or ;
cond_or = cond_and { '||' cond_and } ;
cond_and = cond_not { '&&' cond_not } ;
cond_not = '!' cond_not | cond_prim ;
cond_prim = '(' cond_expr ')'
| UNARY_OP WORD
| WORD BINARY_OP WORD
| WORD ;
UNARY_OP = '-a' | '-b' | '-c' | '-d' | '-e' | '-f' | '-g' | '-G' | '-h'
| '-k' | '-L' | '-n' | '-o' | '-O' | '-p' | '-r' | '-s' | '-S'
| '-t' | '-u' | '-v' | '-w' | '-x' | '-z' ;
BINARY_OP = '=' | '==' | '!=' | '=~' | '<' | '>'
| '-eq' | '-ne' | '-lt' | '-le' | '-gt' | '-ge'
| '-nt' | '-ot' | '-ef' ;</code></pre><p>Newlines are allowed before <span style="display: inline-block">]]</span> and after <span style="display: inline-block">&&</span> and <span style="display: inline-block">||</span>. Inside <span style="display: inline-block">[[ ]]</span>, <span style="display: inline-block"><</span>, <span style="display: inline-block">></span>, <span style="display: inline-block">(</span> and <span style="display: inline-block">)</span> are operators, not redirections or subshells.</p><div>R-CND-1 The operands of <span style="display: inline-block">[[ ]]</span> are expanded without field splitting and without pathname expansion, and only when the evaluation reaches them: <span style="display: inline-block">&&</span> and <span style="display: inline-block">||</span> do not expand an operand they do not need.<br>Tests: <span style="display: inline-block">zash/cond</span></div><div>R-CND-2 <span style="display: inline-block">a = b</span>, <span style="display: inline-block">a == b</span> and <span style="display: inline-block">a != b</span>: <em>b</em> is a pattern (<span style="text-decoration: underline"><a href="#patterns">Section 6</a></span>), with its quoted bytes literal. <span style="display: inline-block">a =~ b</span>: <em>b</em> is a POSIX extended regular expression, matched anywhere in <em>a</em>; quoted parts of <em>b</em> are literal; an invalid expression gives status 2.<br>Tests: <span style="display: inline-block">zash/cond</span></div><div>R-CND-3 <span style="display: inline-block"><</span> and <span style="display: inline-block">></span> compare by byte value. <span style="display: inline-block">-eq</span> and the other numeric operators evaluate both operands as arithmetic expressions (<span style="text-decoration: underline"><a href="#arithmetic">Section 7</a></span>). <span style="display: inline-block">-o name</span> is true if the option is on; <span style="display: inline-block">-v name</span> if the variable is set; the remaining unary operators are those of <span style="display: inline-block">test</span>. A lone <span style="display: inline-block">WORD</span> is true if it is not empty.<br>Tests: <span style="display: inline-block">zash/cond</span></div><div>R-CND-4 <span style="display: inline-block">f1 -nt f2</span> is true if <em>f1</em> exists and <em>f2</em> does not, or both exist and <em>f1</em> is newer; <span style="display: inline-block">-ot</span> is the converse (POSIX.1-2024).<br>Tests: <span style="display: inline-block">zash/ash-differences</span></div><div>R-CND-5 The status of <span style="display: inline-block">[[ e ]]</span> is 0 if <em>e</em> is true, 1 if false, 2 on a syntax or evaluation error.<br>Tests: <span style="display: inline-block">zash/cond</span></div><h2 id="redirection">10 Redirection</h2><div><p>R-RED-1 Redirections are performed left to right. The descriptor <em>n</em> is the <span style="display: inline-block">IO_NUMBER</span> if given, otherwise 0 for <span style="display: inline-block"><</span>, <span style="display: inline-block"><></span>, <span style="display: inline-block"><&</span>, <span style="display: inline-block"><<</span>, <span style="display: inline-block"><<-</span> and 1 for the others. The target word is expanded as one field (<span style="text-decoration: underline"><a href="#expansion">Section 5</a></span>).</p><table><tr><td><span style="display: inline-block">n<f</span></td><td>open <em>f</em> for reading</td></tr><tr><td><span style="display: inline-block">n>f</span></td><td>create or truncate <em>f</em>; an error if <span style="display: inline-block">set -C</span> is on and <em>f</em> is an existing regular file</td></tr><tr><td><span style="display: inline-block">n>|f</span></td><td>as <span style="display: inline-block">></span>, ignoring <span style="display: inline-block">set -C</span></td></tr><tr><td><span style="display: inline-block">n>>f</span></td><td>open <em>f</em> for appending, creating it if it does not exist</td></tr><tr><td><span style="display: inline-block">n<>f</span></td><td>open <em>f</em> for reading and writing, creating it if it does not exist</td></tr><tr><td><span style="display: inline-block">n<&m</span>, <span style="display: inline-block">n>&m</span></td><td>make <em>n</em> a copy of descriptor <em>m</em>; <span style="display: inline-block">-</span> closes <em>n</em></td></tr><tr><td><span style="display: inline-block">n<<d</span>, <span style="display: inline-block">n<<-d</span></td><td>open the here-document body for reading on descriptor <em>n</em></td></tr></table><p><br>Tests: <span style="display: inline-block">posix/redir</span>, <span style="display: inline-block">posix/redir-edge</span></p></div><div>R-RED-2 Descriptors 0 to 9 are available to scripts. A redirection of a larger descriptor, or a duplication from a descriptor that is not open, is a redirection error.<br>Tests: <span style="display: inline-block">posix/fdmap</span></div><div>R-RED-3 A redirection on a compound command or a function definition applies to the whole command each time it runs, and is undone after it.<br>Tests: <span style="display: inline-block">posix/redir</span></div><div>R-RED-4 <span style="display: inline-block">exec</span> with only redirections applies them to the current shell permanently.<br>Tests: <span style="display: inline-block">posix/redir</span></div><h2 id="environment">11 Environment and variables</h2><div>R-ENV-1 A variable has a name, a value (a byte string) and attributes: <em>exported</em>, <em>read-only</em>, <em>integer</em>. Exported variables are passed in the environment of commands the shell runs. At startup every environment entry with a valid name becomes an exported variable.<br>Tests: <span style="display: inline-block">posix/vars</span></div><div>R-ENV-2 Assigning to a read-only variable, or unsetting one, is an error.<br>Tests: <span style="display: inline-block">posix/vars</span></div><div>R-ENV-3 Assigning to an <em>integer</em> variable (<span style="display: inline-block">typeset -i</span>, <span style="display: inline-block">integer</span>) evaluates the value as an arithmetic expression and stores the decimal result.<br>Tests: <span style="display: inline-block">zash/typeset</span>, <span style="display: inline-block">zash/let</span></div><div>R-ENV-4 <span style="display: inline-block">RANDOM</span> yields a pseudo-random integer from 0 to 32767 on each expansion; assigning to it sets the seed. <span style="display: inline-block">SECONDS</span> yields the whole seconds since the shell started, or since it was last assigned plus the assigned value. Unsetting either removes the special behaviour.<br>Tests: <span style="display: inline-block">zash/dynvars</span></div><div>R-ENV-5 A shell started interactively, and not set-user-ID or set-group-ID, expands <span style="display: inline-block">ENV</span> by parameter expansion and reads the resulting file before its first command. A set-ID shell ignores <span style="display: inline-block">ENV</span>.</div><div>R-ENV-6 The options are those of <span style="display: inline-block">set</span>: <span style="display: inline-block">-a -b -C -e -f -h -m -n -u -v -x</span>, and by name <mark>allexport errexit ignoreeof monitor noclobber noexec noglob nolog notify nounset pipefail trackall verbose vi emacs xtrace</mark>. <span style="display: inline-block">notify</span>, <span style="display: inline-block">trackall</span> and <span style="display: inline-block">nolog</span> are accepted and have no effect. The z/OS build adds <span style="display: inline-block">jobnames</span>, <span style="display: inline-block">shareas</span>, <span style="display: inline-block">msgid</span> and <span style="display: inline-block">pipetag</span> (<span style="text-decoration: underline"><a href="#zos">Section 14</a></span>).<br>Tests: <span style="display: inline-block">zash/options</span></div><h2 id="errors">12 Errors</h2><div><p>R-ERR-1 The consequence of an error depends on its kind and on whether the shell is interactive:</p><table><tr><td><strong>error</strong></td><td><strong>non-interactive</strong></td><td><strong>interactive</strong></td></tr><tr><td>syntax error</td><td>exit, status 2</td><td>status 2, next command</td></tr><tr><td>special builtin error</td><td>exit</td><td>status, next command</td></tr><tr><td>redirection error on a special builtin</td><td>exit</td><td>status 2</td></tr><tr><td>redirection error on any other command</td><td>status 2</td><td>status 2</td></tr><tr><td>variable assignment error</td><td>exit</td><td>status, next command</td></tr><tr><td>expansion error</td><td>exit</td><td>status, next command</td></tr><tr><td>command not found</td><td>status 127</td><td>status 127</td></tr></table><p>An error that exits the shell runs the <span style="display: inline-block">EXIT</span> trap first.<br>Tests: <span style="display: inline-block">posix/errexit</span>, <span style="display: inline-block">posix/command-special</span>, <span style="display: inline-block">posix/invoke</span></p></div><div>R-ERR-2 <strong>errexit.</strong> With <span style="display: inline-block">set -e</span>, when a pipeline (or a single command) fails, the shell exits with its status, unless the failing command is: part of the condition of <span style="display: inline-block">if</span>, <span style="display: inline-block">while</span> or <span style="display: inline-block">until</span>; part of an <span style="display: inline-block">&&</span> or <span style="display: inline-block">||</span> list other than the last command; or preceded by <span style="display: inline-block">!</span>. The exemption extends to everything executed within those positions, including function bodies.<br>Tests: <span style="display: inline-block">posix/errexit</span>, <span style="display: inline-block">posix/errexit-edge</span></div><div>R-ERR-3 <span style="display: inline-block">command</span> before a special builtin makes errors inside it (including errors in <span style="display: inline-block">eval</span>ed or <span style="display: inline-block">.</span>-read text) set the status instead of exiting the shell. <span style="display: inline-block">exit</span> still exits.<br>Tests: <span style="display: inline-block">posix/command-special</span></div><div>R-ERR-4 (zash) <strong>ERR trap.</strong> When a command fails in a position where <span style="display: inline-block">errexit</span> would apply, the <span style="display: inline-block">ERR</span> trap, if set, runs before <span style="display: inline-block">errexit</span> exits the shell.<br>Tests: <span style="display: inline-block">zash/errtrap</span></div><h2 id="traps">13 Traps and signals</h2><div>R-SIG-1 <span style="display: inline-block">trap action sig...</span> sets <em>action</em> to run when a signal arrives. The action runs after the command being executed completes, not in the middle of it, and with <span style="display: inline-block">$?</span> equal to that command’s status; afterwards <span style="display: inline-block">$?</span> is restored. An action of <span style="display: inline-block">-</span> resets to the default, and an empty action ignores the signal.<br>Tests: <span style="display: inline-block">posix/traps</span>, <span style="display: inline-block">scripts/signals</span></div><div>R-SIG-2 A trap that becomes due while the shell is reading a command runs before that command and does not disturb the command being read.<br>Tests: <span style="display: inline-block">posix/trap-input</span></div><div>R-SIG-3 A subshell resets traps to their defaults, except that ignored signals stay ignored. <em>Difference: in a pipeline stage or <span style="display: inline-block">$( )</span>, <span style="display: inline-block">trap</span> with no arguments lists the parent shell’s traps until the subshell sets one of its own.</em><br>Tests: <span style="display: inline-block">posix/subshell-edge</span></div><div>R-SIG-4 The <span style="display: inline-block">EXIT</span> trap (signal 0) runs when the shell exits, whether by <span style="display: inline-block">exit</span>, end of input or a fatal error.<br>Tests: <span style="display: inline-block">posix/invoke</span></div><h2 id="zos">14 z/OS</h2><p>These rules hold only for the z/OS builds. They affect only character encoding and process creation, not script semantics.</p><div>R-ENV-7 (z/OS) The build is ASCII (ISO8859-1) or EBCDIC (IBM-1047), fixed at compile time. Script text is interpreted in the build’s character set. An untagged script whose first block looks like the other character set is converted when it is read.</div><div>R-ENV-8 (z/OS) <span style="display: inline-block">set -o jobnames</span>, <span style="display: inline-block">shareas</span>, <span style="display: inline-block">msgid</span>, <span style="display: inline-block">pipetag</span> and the variables <span style="display: inline-block">_TAG_REDIR_IN</span>, <span style="display: inline-block">_TAG_REDIR_OUT</span>, <span style="display: inline-block">_TAG_REDIR_ERR</span>, <span style="display: inline-block">_BPX_SHAREAS</span>, <span style="display: inline-block">_BPX_JOBNAME</span> and <span style="display: inline-block">ZOS_*</span> change process attributes and file tags. They are specified in DESIGN.md, “The z/OS layer”, not here.</div><h2 id="differences">15 Differences from POSIX and other shells</h2><table><tr><td><strong>rule</strong></td><td><strong>difference</strong></td></tr><tr><td><span style="display: inline-block">R-LEX-2</span></td><td>standard input is never read ahead of the running command</td></tr><tr><td><span style="display: inline-block">R-LEX-11</span></td><td><span style="display: inline-block">$'...'</span> escapes, <span style="display: inline-block">\c</span><em>X</em> in ASCII builds only</td></tr><tr><td><span style="display: inline-block">R-LEX-12</span></td><td><span style="display: inline-block">function</span>, <span style="display: inline-block">time</span>, <span style="display: inline-block">[[</span>, <span style="display: inline-block">]]</span> are reserved</td></tr><tr><td><span style="display: inline-block">R-GRM-3</span></td><td><span style="display: inline-block">function</span> form has no ksh-style local traps or <span style="display: inline-block">$0</span></td></tr><tr><td><span style="display: inline-block">R-GRM-6</span></td><td>heuristic choice between <span style="display: inline-block">((</span> and <span style="display: inline-block">( (</span></td></tr><tr><td><span style="display: inline-block">R-PAT-2</span></td><td><span style="display: inline-block">[^...]</span> negates</td></tr><tr><td><span style="display: inline-block">R-PAT-3</span></td><td>ranges and sorting by byte code, not locale</td></tr><tr><td><span style="display: inline-block">R-ARI-6</span></td><td><span style="display: inline-block">++</span>, <span style="display: inline-block">--</span> and <span style="display: inline-block">,</span></td></tr><tr><td><span style="display: inline-block">R-ARI-5</span></td><td>variable values evaluated as expressions, 16 levels deep</td></tr><tr><td><span style="display: inline-block">R-CMD-9</span></td><td>last pipeline stage in the current shell</td></tr><tr><td><span style="display: inline-block">R-CMD-21</span></td><td><span style="display: inline-block">return 300</span> gives 44</td></tr><tr><td><span style="display: inline-block">R-CMD-22</span></td><td><span style="display: inline-block">local</span>, <span style="display: inline-block">typeset</span> with dynamic scope</td></tr><tr><td><span style="display: inline-block">R-CND-4</span></td><td><span style="display: inline-block">-nt</span> true against a missing file</td></tr><tr><td><span style="display: inline-block">R-ERR-4</span></td><td><span style="display: inline-block">ERR</span> trap</td></tr><tr><td><span style="display: inline-block">R-SIG-3</span></td><td><span style="display: inline-block">trap</span> in a subshell lists the parent’s traps</td></tr></table><div><strong>Note.</strong> The identifiers in this table are looked up from the rules themselves, so they stay right when rules are added.</div><h2 id="loc-26">16 Mapping to a formal model</h2><p>Each chapter becomes one Lean module. The first things to formalise:</p><table><tr><td><strong>chapter</strong></td><td><strong>what to formalise first</strong></td></tr><tr><td>Patterns</td><td><span style="display: inline-block">match</span> as an inductive predicate (<span style="display: inline-block">R-PAT-4</span>); prove the trimming rule (<span style="display: inline-block">R-EXP-9</span>) correct for the shortest and longest match</td></tr><tr><td>Arithmetic</td><td>the grammar as an inductive type and <math><mrow><mo>⟦</mo><mi>𝑒</mi><mo>⟧</mo></mrow></math> as a total function into <span style="display: inline-block">Int64</span>; prove <span style="display: inline-block">R-ARI-1</span> (wrapping) and <span style="display: inline-block">R-ARI-4</span> (unevaluated operands have no effect)</td></tr><tr><td>Field splitting</td><td><span style="display: inline-block">R-EXP-15</span> as a function on flagged bytes; prove that quoted bytes are never split (<span style="display: inline-block">R-EXP-1</span>)</td></tr><tr><td>Grammar</td><td>the productions as an inductive type; the <span style="display: inline-block">((</span> rule (<span style="display: inline-block">R-GRM-6</span>) as a decidable predicate</td></tr><tr><td>Commands</td><td>a small-step semantics over an environment; last-stage and subshell isolation as theorems (<span style="display: inline-block">R-CMD-9</span>, <span style="display: inline-block">R-CMD-14</span>)</td></tr></table><p>Differential testing then checks the C code against the model, with the existing suite and random inputs. CBMC checks the smallest pieces.</p></body></html>
|