| 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 - runtime error: cannot assign list to array number |
| 53 | # mksh - a[-1]+: is not an identifier |
| 54 | # osh - parse error -- could be better! |
| 55 | a=(1 '2 3') |
| 56 | a[-1]+=(4 5) |
| 57 | argv.py "${a[@]}" |
| 58 | ## OK bash STDOUT: |
| 59 | ['1', '2 3'] |
| 60 | ## END |
| 61 | ## OK bash status: 0 |
| 62 | ## N-I mksh stdout-json: "" |
| 63 | ## N-I mksh status: 1 |
| 64 | ## OK stdout-json: "" |
| 65 | ## OK osh status: 2 |
| 66 | |
| 67 | #### Strings have value semantics, not reference semantics |
| 68 | s1='abc' |
| 69 | s2=$s1 |
| 70 | s1+='d' |
| 71 | echo $s1 $s2 |
| 72 | ## stdout: abcd abc |
| 73 | |
| 74 | #### Append to nonexistent string |
| 75 | f() { |
| 76 | local a+=a |
| 77 | echo $a |
| 78 | |
| 79 | b+=b |
| 80 | echo $b |
| 81 | |
| 82 | readonly c+=c |
| 83 | echo $c |
| 84 | |
| 85 | export d+=d |
| 86 | echo $d |
| 87 | |
| 88 | # Not declared anywhere |
| 89 | e[1]+=e |
| 90 | echo ${e[1]} |
| 91 | |
| 92 | # Declare is the same, but mksh doesn't support it |
| 93 | #declare e+=e |
| 94 | #echo $e |
| 95 | } |
| 96 | f |
| 97 | ## STDOUT: |
| 98 | a |
| 99 | b |
| 100 | c |
| 101 | d |
| 102 | e |
| 103 | ## END |
| 104 | |
| 105 | # += is invalid on assignment builtins |
| 106 | ## OK osh stdout-json: "" |
| 107 | ## OK osh status: 1 |
| 108 | |
| 109 | |
| 110 | #### Append to nonexistent array is allowed |
| 111 | |
| 112 | ## TODO: strict-array could get rid of this? |
| 113 | y+=(c d) |
| 114 | argv.py "${y[@]}" |
| 115 | ## STDOUT: |
| 116 | ['c', 'd'] |
| 117 | ## END |
| 118 | |
| 119 | #### Append used like env prefix is a parse error |
| 120 | # This should be an error in other shells but it's not. |
| 121 | A=a |
| 122 | A+=a printenv.py A |
| 123 | ## status: 2 |
| 124 | ## BUG bash stdout: aa |
| 125 | ## BUG bash status: 0 |
| 126 | ## BUG mksh stdout: a |
| 127 | ## BUG mksh status: 0 |