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