@@ -45,21 +45,21 @@ cleared before starting the modifications. When the assignment is performed
45
45
with ` += ` , the modifications are applied to the existing content of the LHS
46
46
value.
47
47
48
- An initializer list has the following form: ` (<words>...) ` , where each word has
49
- one of the following forms:
50
-
51
- - ` [<key>]=<value> ` ... This assigns ` <value> ` to an element of the LHS
52
- specified by ` <key> ` . The ` <value> ` is not subject to word splitting and
53
- pathname expansions as if it is the RHS of an assignment.
54
- - ` [<key> ]+=<value> ` ... This appends ` <value> ` to an element of the LHS
55
- specified by ` <key> ` . If the corresponding element does not exist, it simply
56
- assigns ` <value> ` to a new element associated with ` <key> ` . The ` <value> ` is
57
- not subject to word splitting and pathname expansions.
58
- - ` <value> ` ... If the word does not have the above two forms, it is considered
59
- a normal word. In this case, this assigns ` <value> ` to the * next* element,
60
- where the next element is determined by the LHS. Unlike the previous two
61
- forms, the ` <value> ` is subject to word splitting and pathname expansions as
62
- if it is a normal argument to a command.
48
+ An initializer list has the following form: ` '(' ITEMS* ')' ` , where each item
49
+ has one of the following forms:
50
+
51
+ - ` [KEY]=VALUE ` ... This assigns ` VALUE ` to an element of the LHS specified by
52
+ ` KEY ` . The ` VALUE ` is not subject to word splitting and pathname expansions
53
+ as if it is the RHS of an assignment.
54
+ - ` [KEY ]+=VALUE ` ... This appends ` VALUE ` to an element of the LHS specified by
55
+ ` KEY ` . If the corresponding element does not exist, it simply assigns
56
+ ` VALUE ` to a new element associated with ` KEY ` . The ` VALUE ` is not subject
57
+ to word splitting and pathname expansions.
58
+ - ` VALUE ` ... If the item does not have the above two forms, it is considered a
59
+ normal word. In this case, this assigns ` VALUE ` to the * next* element, where
60
+ the next element is determined by the LHS. Unlike the previous two forms,
61
+ the ` VALUE ` is subject to word splitting and pathname expansions as if it is
62
+ a normal argument to a command.
63
63
64
64
The above three forms can be mixed within one initializer list, though there
65
65
may be additional limitations depending on the type of the LHS of the
@@ -73,21 +73,20 @@ applied.
73
73
In the first phase, the type adjustment is performed in the following way:
74
74
75
75
- When the LHS variable is unset, the assignment creates an empty indexed array
76
- (BashArray) and apply the initializer list to the created array . If the
76
+ (BashArray). If the
77
77
assignment is performed through an assignment builtin and flag ` -A ` is
78
78
supplied to the builtin, an empty associative array (BashAssoc) is created
79
79
instead of an empty BashArray.
80
80
- When the LHS is a scalar string, the assignment creates a BashArray with one
81
81
element, where the original value is stored at index ` 0 ` . If the assignment
82
82
is performed through an assignment builtin and flag ` -A ` is supplied to the
83
83
builtin, the assignment creates a BashAssoc with one element, where the
84
- original value is stored at key ` "0" ` instead of a BashArray. If the
84
+ original value is stored at key ` "0" ` , instead of a BashArray. If the
85
85
assignment operator is ` += ` , OSH issues an error "Can't append an array to
86
86
string", while Bash is permissive.
87
- - When the LHS is an indexed or associative arrays, the initializer list is
88
- applied to the original array following [ sh-array] ( #sh-array ) (for an indexed
89
- array) or [ sh-assoc] ( #sh-assoc ) (for an associative array). If the
90
- assignemnt is performed through an assignment builtin and mismatching flag
87
+ - When the LHS is an indexed or associative arrays, the original array is
88
+ directly used for the modification target. If the
89
+ assignment is performed through an assignment builtin and mismatching flag
91
90
(i.e., ` -A ` and ` -a ` for BashArray and BashAssoc, respectively) is supplied,
92
91
OSH discards the original array and creates a new empty BashArray (for flag
93
92
` -a ` ) or BashAssoc (for flag ` -A ` ), while Bash issues an error preserving the
@@ -116,7 +115,7 @@ These rules are summarized in the following table.
116
115
- tr
117
116
- <!-- empty -->
118
117
- ` -A `
119
- - an empty BashArray
118
+ - an empty BashAssoc
120
119
- <!-- empty -->
121
120
- tr
122
121
- Str
@@ -133,11 +132,6 @@ These rules are summarized in the following table.
133
132
- ` -A `
134
133
- BashAssoc with one element, with the original string at key ` "0" `
135
134
- OSH does not accept ` += ` , so the element is never used
136
- - tr
137
- - <!-- empty -->
138
- - ` -A `
139
- - an empty BashArray
140
- - <!-- empty -->
141
135
- tr
142
136
- BashArray
143
137
- (none)
@@ -168,12 +162,17 @@ These rules are summarized in the following table.
168
162
- ` -A `
169
163
- the original BashAssoc
170
164
- <!-- empty -->
165
+ - tr
166
+ - (others)
167
+ - <!-- empty -->
168
+ - N/A
169
+ - Error
171
170
172
171
</table >
173
172
174
173
In the second phase, the modifications are applied depending on the result of
175
174
the first phase. When the result is BashArray, see [ sh-array] ( #sh-array ) .
176
- When the result is BashAssoc, see [ sh-array ] ( #sh-array ) .
175
+ When the result is BashAssoc, see [ sh-assoc ] ( #sh-assoc ) .
177
176
178
177
### sh-array
179
178
@@ -191,14 +190,14 @@ cleared if the assignment operator is `=`. Then, an element of the array is
191
190
modified for each item in the initializer list in order. The index of the
192
191
element to be modified is determined in the following way:
193
192
194
- - When the first initializer item does not have ` [<key> ]= ` or ` [<key> ]+= ` , the
193
+ - When the first initializer item does not have ` [KEY ]= ` or ` [KEY ]+= ` , the
195
194
index is the maximum existing index in the array plus one, or ` 0 ` if the
196
195
array is empty.
197
- - When the second or later initializer item does not have ` [<key> ]= ` or
198
- ` [<key> ]+= ` , the index is larger by one than the one modified by the previous
196
+ - When the second or later initializer item does not have ` [KEY ]= ` or
197
+ ` [KEY ]+= ` , the index is larger by one than the one modified by the previous
199
198
initializer item.
200
- - When the initialier item has ` [<key> ]= ` or ` [<key> ]+= ` , an arithmetic
201
- evaluation is applied to ` <key> ` to obtain the index in ` BigInt `
199
+ - When the initializer item has ` [KEY ]= ` or ` [KEY ]+= ` , an arithmetic evaluation
200
+ is applied to ` KEY ` to obtain the index in ` BigInt `
202
201
203
202
Here are examples:
204
203
@@ -210,9 +209,9 @@ Here are examples:
210
209
declare -a a=([k]=v 2) # This creates a sparse array with two elements,
211
210
# ([10]=v [11]=2)
212
211
213
- declare -a a +=(3 4) # This appends two values to the existing array:
212
+ a +=(3 4) # This appends two values to the existing array:
214
213
# ([10]=v [11]=2 [12]=3 [13]=4)
215
- declare -a a +=([k]=5 6) # This overwrites two elements in the existing
214
+ a +=([k]=5 6) # This overwrites two elements in the existing
216
215
# array: ([10]=5 [11]=6 [12]=3 [13]=4)
217
216
218
217
In YSH, use a [ list-literal] [ ] to create a [ List] [ ] instance.
@@ -236,8 +235,8 @@ The initialization/mutation of BashAssoc is performed in a manner similar to
236
235
BashArray. The associative array is first cleared if the assignment operator
237
236
is ` = ` . Then, the modification of an element is performed for each initializer
238
237
item in order. An item in the initializer list must be in the forms
239
- ` [<key>]=<value> ` or ` [<key>]=<value> ` . The element to be modified is
240
- specified by ` <key> ` .
238
+ ` [KEY]=VALUE ` or ` [KEY]=VALUE ` . The element to be modified is specified by
239
+ ` KEY ` .
241
240
242
241
declare -A a # This creates an empty BashAssoc (OSH)
243
242
declare -A a=() # This creates an empty BashAssoc
@@ -249,9 +248,9 @@ specified by `<key>`.
249
248
declare -A a=([k]=v) # This creates a BashAssoc with one element,
250
249
# (['k']=1). Unlike BashArray, "k" is not
251
250
# processed by arithmetic expansion.
252
- declare -a a +=([a]=3 [b]=4) # This adds two elements to the existing array.
251
+ a +=([a]=3 [b]=4) # This adds two elements to the original array.
253
252
# The result is ([a]=3 [b]=4 [k]=v)
254
- declare -a a +=([k]=5) # This overwrites an element in the existing
253
+ a +=([k]=5) # This overwrites an element in the original
255
254
# array. The result is ([a]=3 [b]=4 [k]=5).
256
255
257
256
In YSH, use a [ dict-literal] [ ] to create a [ Dict] [ ] instance.
0 commit comments