1
2 #### Append string to string
3 s='abc'
4 s+=d
5 echo $s
6 ## stdout: abcd
7
8 #### Append array to array
9 a=(x y )
10 a+=(t 'u v')
11 argv.py "${a[@]}"
12 ## stdout: ['x', 'y', 't', 'u v']
13
14 #### Append array to string should be an error
15 s='abc'
16 s+=(d e f)
17 echo $s
18 ## BUG bash/mksh stdout: abc
19 ## BUG bash/mksh status: 0
20 ## status: 1
21
22 #### Append string to array should be disallowed
23 # They treat this as implicit index 0. We disallow this on the LHS, so we will
24 # also disallow it on the RHS.
25 a=(x y )
26 a+=z
27 argv.py "${a[@]}"
28 ## OK bash/mksh stdout: ['xz', 'y']
29 ## OK bash/mksh status: 0
30 ## status: 1
31
32 #### Append string to array element
33 # They treat this as implicit index 0. We disallow this on the LHS, so we will
34 # also disallow it on the RHS.
35 a=(x y )
36 a[1]+=z
37 argv.py "${a[@]}"
38 ## stdout: ['x', 'yz']
39 ## status: 0
40
41 #### Append to last element
42 # Works in bash, but not mksh. It seems like bash is doing the right thing.
43 # a[-1] is allowed on the LHS. mksh doesn't have negative indexing?
44 a=(1 '2 3')
45 a[-1]+=' 4'
46 argv.py "${a[@]}"
47 ## stdout: ['1', '2 3 4']
48 ## BUG mksh stdout: ['1', '2 3', ' 4']
49
50 #### Try to append list to element
51 # bash - runtime error: cannot assign list to array number
52 # mksh - a[-1]+: is not an identifier
53 # osh - parse error -- could be better!
54 a=(1 '2 3')
55 a[-1]+=(4 5)
56 argv.py "${a[@]}"
57 ## OK bash STDOUT:
58 ['1', '2 3']
59 ## END
60 ## OK bash status: 0
61 ## N-I mksh stdout-json: ""
62 ## N-I mksh status: 1
63 ## OK stdout-json: ""
64 ## OK osh status: 2
65
66 #### Strings have value semantics, not reference semantics
67 s1='abc'
68 s2=$s1
69 s1+='d'
70 echo $s1 $s2
71 ## stdout: abcd abc
72
73 #### Append to nonexistent string
74 f() {
75 local a+=a
76 echo $a
77
78 b+=b
79 echo $b
80
81 readonly c+=c
82 echo $c
83
84 export d+=d
85 echo $d
86
87 # Not declared anywhere
88 e[1]+=e
89 echo ${e[1]}
90
91 # Declare is the same, but mksh doesn't support it
92 #declare e+=e
93 #echo $e
94 }
95 f
96 ## STDOUT:
97 a
98 b
99 c
100 d
101 e
102 ## END
103
104 # += is invalid on assignment builtins
105 ## OK osh stdout-json: ""
106 ## OK osh status: 1
107
108
109 #### Append to nonexistent array is allowed
110
111 ## TODO: strict_array could get rid of this?
112 y+=(c d)
113 argv.py "${y[@]}"
114 ## STDOUT:
115 ['c', 'd']
116 ## END
117
118 #### Append used like env prefix is a parse error
119 # This should be an error in other shells but it's not.
120 A=a
121 A+=a printenv.py A
122 ## status: 2
123 ## BUG bash stdout: aa
124 ## BUG bash status: 0
125 ## BUG mksh stdout: a
126 ## BUG mksh status: 0