| 1 | ## compare_shells: bash mksh
|
| 2 | ## oils_failures_allowed: 2
|
| 3 |
|
| 4 | # Arrays decay upon assignment (without splicing) and equality. This will not
|
| 5 | # be true in Oil -- arrays will be first class.
|
| 6 |
|
| 7 | #### Assignment Causes Array Decay
|
| 8 | set -- x y z
|
| 9 | argv.py "[$@]"
|
| 10 | var="[$@]"
|
| 11 | argv.py "$var"
|
| 12 | ## STDOUT:
|
| 13 | ['[x', 'y', 'z]']
|
| 14 | ['[x y z]']
|
| 15 | ## END
|
| 16 |
|
| 17 | #### Array Decay with IFS
|
| 18 | IFS=x
|
| 19 | set -- x y z
|
| 20 | var="[$@]"
|
| 21 | argv.py "$var"
|
| 22 | ## stdout: ['[x y z]']
|
| 23 |
|
| 24 | #### User arrays decay
|
| 25 | declare -a a b
|
| 26 | a=(x y z)
|
| 27 | b="${a[@]}" # this collapses to a string
|
| 28 | c=("${a[@]}") # this preserves the array
|
| 29 | c[1]=YYY # mutate a copy -- doesn't affect the original
|
| 30 | argv.py "${a[@]}"
|
| 31 | argv.py "${b}"
|
| 32 | argv.py "${c[@]}"
|
| 33 | ## STDOUT:
|
| 34 | ['x', 'y', 'z']
|
| 35 | ['x y z']
|
| 36 | ['x', 'YYY', 'z']
|
| 37 | ## END
|
| 38 |
|
| 39 | #### strict_array: $array is not valid in OSH, is ${array[0]} in ksh/bash
|
| 40 | shopt -s strict_array
|
| 41 |
|
| 42 | a=(1 '2 3')
|
| 43 | echo $a
|
| 44 | ## STDOUT:
|
| 45 | 1
|
| 46 | ## END
|
| 47 | ## OK osh status: 1
|
| 48 | ## OK osh stdout-json: ""
|
| 49 |
|
| 50 | #### strict_array: ${array} is not valid in OSH, is ${array[0]} in ksh/bash
|
| 51 | shopt -s strict_array
|
| 52 |
|
| 53 | a=(1 '2 3')
|
| 54 | echo ${a}
|
| 55 | ## STDOUT:
|
| 56 | 1
|
| 57 | ## END
|
| 58 | ## OK osh status: 1
|
| 59 | ## OK osh stdout-json: ""
|
| 60 |
|
| 61 | #### Assign to array index without initialization
|
| 62 | a[5]=5
|
| 63 | a[6]=6
|
| 64 | echo "${a[@]}" ${#a[@]}
|
| 65 | ## stdout: 5 6 2
|
| 66 |
|
| 67 | #### a[40] grows array
|
| 68 | a=(1 2 3)
|
| 69 | a[1]=5
|
| 70 | a[40]=30 # out of order
|
| 71 | a[10]=20
|
| 72 | echo "${a[@]}" "${#a[@]}" # length is 1
|
| 73 | ## stdout: 1 5 3 20 30 5
|
| 74 |
|
| 75 | #### array decays to string when comparing with [[ a = b ]]
|
| 76 | a=('1 2' '3 4')
|
| 77 | s='1 2 3 4' # length 2, length 4
|
| 78 | echo ${#a[@]} ${#s}
|
| 79 | [[ "${a[@]}" = "$s" ]] && echo EQUAL
|
| 80 | ## STDOUT:
|
| 81 | 2 7
|
| 82 | EQUAL
|
| 83 | ## END
|
| 84 |
|
| 85 | #### ++ on a whole array increments the first element (disallowed with strict_array)
|
| 86 | shopt -s strict_array
|
| 87 |
|
| 88 | a=(1 10)
|
| 89 | (( a++ )) # doesn't make sense
|
| 90 | echo "${a[@]}"
|
| 91 | ## stdout: 2 10
|
| 92 | ## OK osh status: 1
|
| 93 | ## OK osh stdout-json: ""
|
| 94 |
|
| 95 | #### Apply vectorized operations on ${a[*]}
|
| 96 | a=('-x-' 'y-y' '-z-')
|
| 97 |
|
| 98 | # This does the prefix stripping FIRST, and then it joins.
|
| 99 | argv.py "${a[*]#-}"
|
| 100 | ## STDOUT:
|
| 101 | ['x- y-y z-']
|
| 102 | ## END
|
| 103 | ## N-I mksh status: 1
|
| 104 | ## N-I mksh stdout-json: ""
|
| 105 |
|
| 106 | #### value.BashArray internal representation - Indexed
|
| 107 |
|
| 108 | case $SH in mksh) exit ;; esac
|
| 109 |
|
| 110 | z=()
|
| 111 | declare -a | grep z=
|
| 112 |
|
| 113 | z+=(b c)
|
| 114 | declare -a | grep z=
|
| 115 |
|
| 116 | # z[5]= finds the index, or puts it in SORTED order I think
|
| 117 | z[5]=d
|
| 118 | declare -a | grep z=
|
| 119 |
|
| 120 | z[1]=ZZZ
|
| 121 | declare -a | grep z=
|
| 122 |
|
| 123 | # Adds after last index
|
| 124 | z+=(f g)
|
| 125 | declare -a | grep z=
|
| 126 |
|
| 127 | # This is the equivalent of z[0]+=mystr
|
| 128 | z+=-mystr
|
| 129 | declare -a | grep z=
|
| 130 |
|
| 131 | z[1]+=-append
|
| 132 | declare -a | grep z=
|
| 133 |
|
| 134 | argv.py keys "${!z[@]}" # 0 1 5 6 7
|
| 135 | argv.py values "${z[@]}"
|
| 136 |
|
| 137 | # can't do this conversion
|
| 138 | declare -A z
|
| 139 | declare -A | grep z=
|
| 140 |
|
| 141 | echo status=$?
|
| 142 |
|
| 143 | ## STDOUT:
|
| 144 | declare -a z=()
|
| 145 | declare -a z=([0]="b" [1]="c")
|
| 146 | declare -a z=([0]="b" [1]="c" [5]="d")
|
| 147 | declare -a z=([0]="b" [1]="ZZZ" [5]="d")
|
| 148 | declare -a z=([0]="b" [1]="ZZZ" [5]="d" [6]="f" [7]="g")
|
| 149 | declare -a z=([0]="b-mystr" [1]="ZZZ" [5]="d" [6]="f" [7]="g")
|
| 150 | declare -a z=([0]="b-mystr" [1]="ZZZ-append" [5]="d" [6]="f" [7]="g")
|
| 151 | ['keys', '0', '1', '5', '6', '7']
|
| 152 | ['values', 'b-mystr', 'ZZZ-append', 'd', 'f', 'g']
|
| 153 | status=1
|
| 154 | ## END
|
| 155 |
|
| 156 | ## N-I mksh STDOUT:
|
| 157 | ## END
|
| 158 |
|
| 159 | #### value.BashArray internal representation - Assoc (ordering is a problem)
|
| 160 |
|
| 161 | case $SH in mksh) exit ;; esac
|
| 162 |
|
| 163 | declare -A A=([k]=v)
|
| 164 | declare -A | grep A=
|
| 165 |
|
| 166 | argv.py keys "${!A[@]}"
|
| 167 | argv.py values "${A[@]}"
|
| 168 |
|
| 169 | exit
|
| 170 |
|
| 171 | # Huh this actually works, we don't support it
|
| 172 | # Hm the order here is all messed up, in bash 5.2
|
| 173 | A+=([k2]=v2 [0]=foo [9]=9 [9999]=9999)
|
| 174 | declare -A | grep A=
|
| 175 |
|
| 176 | A+=-append
|
| 177 | declare -A | grep A=
|
| 178 |
|
| 179 | argv.py keys "${!A[@]}"
|
| 180 | argv.py values "${A[@]}"
|
| 181 |
|
| 182 | ## STDOUT:
|
| 183 | declare -A A=([k]="v" )
|
| 184 | ['keys', 'k']
|
| 185 | ['values', 'v']
|
| 186 | ## END
|
| 187 |
|
| 188 | ## N-I mksh STDOUT:
|
| 189 | ## END
|