| 1 | #!/bin/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 |