11. Bash V2 grammarΒΆ

This grammar is defined with the BNF syntax for context-free grammars. The syntax is valid for US-ASCII encoded text.

  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
166
167
168
169
170
171
172
173
<ALPHA> ::= a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|
             A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z

<DIGIT> ::= 0|1|2|3|4|5|6|7|8|9

<NUMBER> ::= <DIGIT>
           | <NUMBER> <DIGIT>

<WORD> ::= <ALPHA>
         | <WORD> <ALPHA>
         | <WORD> '_'

<WORD-LIST> ::= <WORD>
             |  <WORD-LIST> <WORD>

<ASSIGNMENT-WORD> ::= <WORD> '=' <WORD>


<REDIRECTION> ::=  '>' <WORD>
                |  '<' <WORD>
                |  <NUMBER> '>' <WORD>
                |  <NUMBER> '<' <WORD>
                |  '>>' <WORD>
                |  <NUMBER> '>>' <WORD>
                |  '<<' <WORD>
                |  <NUMBER> '<<' <WORD>
                |  '<&' <NUMBER>
                |  <NUMBER> '<&' <NUMBER>
                |  '>&' <NUMBER>
                |  <NUMBER> '>&' <NUMBER>
                |  '<&' <WORD>
                |  <NUMBER> '<&' <WORD>
                |  '>&' <WORD>
                |  <NUMBER> '>&' <WORD>
                |  '<<-' <WORD>
                |  <NUMBER> '<<-' <WORD>
                |  '>&' '-'
                |  <NUMBER> '>&' '-'
                |  '<&' '-'
                |  <NUMBER> '<&' '-'
                |  '&>' <WORD>
                |  <NUMBER> '<>' <WORD>
                |  '<>' <WORD>
                |  '>|' <WORD>
                |  <NUMBER> '>|' <WORD>

<SIMPLE-COMMAND-ELEMENT> ::= <WORD>
                          |  <ASSIGNMENT-WORD>
                          |  <REDIRECTION>

<REDIRECTION-LIST> ::= <REDIRECTION>
                    |  <REDIRECTION-LIST> <REDIRECTION>

<SIMPLE-COMMAND> ::=  <SIMPLE-COMMAND-ELEMENT>
                   |  <SIMPLE-COMMAND> <SIMPLE-COMMAND-ELEMENT>

<COMMAND> ::=  <SIMPLE-COMMAND>
            |  <SHELL-COMMAND>
            |  <SHELL-COMMAND> <REDIRECTION-LIST>

<SHELL-COMMAND> ::=  <FOR-COMMAND>
                  |  <CASE-COMMAND>
                  |  while <COMPOUND-LIST> do <COMPOUND-LIST> done
                  |  until <COMPOUND-LIST> do <COMPOUND-LIST> done
                  |  <SELECT-COMMAND>
                  |  <IF-COMMAND>
                  |  <SUBSHELL>
                  |  <GROUP-COMMAND>
                  |  <FUNCTION-DEF>

<FOR-COMMAND> ::=  for <WORD> <NEWLINE-LIST> do <COMPOUND-LIST> done
            |  for <WORD> <NEWLINE-LIST> '{' <COMPOUND-LIST> '}'
            |  for <WORD> ';' <NEWLINE-LIST> do <COMPOUND-LIST> done
            |  for <WORD> ';' <NEWLINE-LIST> '{' <COMPOUND-LIST> '}'
            |  for <WORD> <NEWLINE-LIST> in <WORD-LIST> <LIST-TERMINATOR>
                   <NEWLINE-LIST> do <COMPOUND-LIST> done
            |  for <WORD> <NEWLINE-LIST> in <WORD-LIST> <LIST-TERMINATOR>
                   <NEWLINE-LIST> '{' <COMPOUND-LIST> '}'

<SELECT-COMMAND> ::=  select <WORD> <NEWLINE-LIST> do <LIST> done
                   |  select <WORD> <NEWLINE-LIST> '{' <LIST> '}'
                   |  select <WORD> ';' <NEWLINE-LIST> do <LIST> done
                   |  select <WORD> ';' <NEWLINE-LIST> '{' LIST '}'
                   |  select <WORD> <NEWLINE-LIST> in <WORD-LIST>
                           <LIST-TERMINATOR> <NEWLINE-LIST> do <LIST> done
                   |  select <WORD> <NEWLINE-LIST> in <WORD-LIST>
                           <LIST-TERMINATOR> <NEWLINE-LIST> '{' <LIST> '}'

<CASE-COMMAND> ::=  case <WORD> <NEWLINE-LIST> in <NEWLINE-LIST> esac
                 |  case <WORD> <NEWLINE-LIST> in <CASE-CLAUSE-SEQUENCE>
                         <NEWLINE-LIST> esac
                 |  case <WORD> <NEWLINE-LIST> in <CASE-CLAUSE> esac

<FUNCTION-DEF> ::=  <WORD> '(' ')' <NEWLINE-LIST> <GROUP-COMMAND>
                 |  function <WORD> '(' ')' <NEWLINE-LIST> <GROUP-COMMAND>
                 |  function <WORD> <NEWLINE-LIST> <GROUP-COMMAND>

<SUBSHELL> ::=  '(' <COMPOUND-LIST> ')'

<IF-COMMAND> ::= if <COMPOUND-LIST> then <COMPOUND-LIST> fi
          | if <COMPOUND-LIST> then <COMPOUND-LIST> else <COMPOUND-LIST> fi
          | if <COMPOUND-LIST> then <COMPOUND-LIST> <ELIF-CLAUSE> fi

<GROUP-COMMAND> ::=  '{' <LIST> '}'

<ELIF-CLAUSE> ::= elif <COMPOUND-LIST> then <COMPOUND-LIST>
           | elif <COMPOUND-LIST> then <COMPOUND-LIST> else <COMPOUND-LIST>
           | elif <COMPOUND-LIST> then <COMPOUND-LIST> <ELIF-CLAUSE>

<CASE-CLAUSE> ::=  <PATTERN-LIST>
                |  <CASE-CLAUSE-SEQUENCE> <PATTERN-LIST>

<PATTERN-LIST> ::=  <NEWLINE-LIST> <PATTERN> ')' <COMPOUND-LIST>
                 |  <NEWLINE-LIST> <PATTERN> ')' <NEWLINE-LIST>
                 |  <NEWLINE-LIST> '(' <PATTERN> ')' <COMPOUND-LIST>
                 |  <NEWLINE-LIST> '(' <PATTERN> ')' <NEWLINE-LIST>

<CASE-CLAUSE-SEQUENCE> ::=  <PATTERN-LIST> ';;'
                         |  <CASE-CLAUSE-SEQUENCE> <PATTERN-LIST> ';;'

<PATTERN> ::=  <WORD>
            |  <PATTERN> '|' <WORD>


<LIST> ::=   <NEWLINE-LIST> <LIST0>

<COMPOUND-LIST> ::=  <LIST>
                  |  <NEWLINE-LIST> <LIST1>

<LIST0> ::=   <LIST1> '\n' <NEWLINE-LIST>
           |  <LIST1> '&' <NEWLINE-LIST>
           |  <LIST1> ';' <NEWLINE-LIST>

<LIST1> ::=   <LIST1> '&&' <NEWLINE-LIST> <LIST1>
           |  <LIST1> '||' <NEWLINE-LIST> <LIST1>
           |  <LIST1> '&' <NEWLINE-LIST> <LIST1>
           |  <LIST1> ';' <NEWLINE-LIST> <LIST1>
           |  <LIST1> '\n' <NEWLINE-LIST> <LIST1>
           |  <PIPELINE-COMMAND>

<LIST-TERMINATOR> ::= '\n'
                   |  ';'

<NEWLINE-LIST> ::=
                  |  <NEWLINE-LIST> '\n'

<SIMPLE-LIST> ::=  <SIMPLE-LIST1>
                |  <SIMPLE-LIST1> '&'
                |  <SIMPLE-LIST1> ';'

<SIMPLE-LIST1> ::=  <SIMPLE-LIST1> '&&' <NEWLINE-LIST> <SIMPLE-LIST1>
                 |  <SIMPLE-LIST1> '||' <NEWLINE-LIST> <SIMPLE-LIST1>
                 |  <SIMPLE-LIST1> '&' <SIMPLE-LIST1>
                 |  <SIMPLE-LIST1> ';' <SIMPLE-LIST1>
                 |  <PIPELINE-COMMAND>

<PIPELINE-COMMAND> ::= <PIPELINE>
                    |  '!' <PIPELINE>
                    |  <TIMESPEC> <PIPELINE>
                    |  <TIMESPEC> '!' <PIPELINE>
                    |  '!' <TIMESPEC> <PIPELINE>

<PIPELINE> ::=
          <PIPELINE> '|' <NEWLINE-LIST> <PIPELINE>
       |  <COMMAND>

<TIME-OPT> ::= '-p'

<TIMESPEC> ::=  time
             |  time <TIME-OPT>

.XE "BNF (Backus-Naur Form)"
.XE "bash" "syntax, BNF form of"