1 ## compare_shells: bash-4.4
2
3 # Tests for bash's type flags on cells. Hopefully we don't have to implement
4 # this, but it's good to know the behavior.
5 #
6 # OSH follows a Python-ish model of types carried with values/objects, not
7 # locations.
8 #
9 # See https://github.com/oilshell/oil/issues/26
10
11
12 #### declare -i -l -u errors can be silenced - ignore_flags_not_impl
13
14 declare -i foo=2+3
15 echo status=$?
16 echo foo=$foo
17 echo
18
19 shopt -s ignore_flags_not_impl
20 declare -i bar=2+3
21 echo status=$?
22 echo bar=$bar
23
24 ## STDOUT:
25 status=2
26 foo=
27
28 status=0
29 bar=2+3
30 ## END
31
32 # bash doesn't need this
33
34 ## OK bash STDOUT:
35 status=0
36 foo=5
37
38 status=0
39 bar=5
40 ## END
41
42 #### declare -i with +=
43 declare s
44 s='1 '
45 s+=' 2 ' # string append
46
47 declare -i i
48 i='1 '
49 i+=' 2 ' # arith add
50
51 declare -i j
52 j=x # treated like zero
53 j+=' 2 ' # arith add
54
55 echo "[$s]"
56 echo [$i]
57 echo [$j]
58 ## STDOUT:
59 [1 2 ]
60 [3]
61 [2]
62 ## END
63 ## N-I osh STDOUT:
64 [1 2 ]
65 [1 2 ]
66 [x 2 ]
67 ## END
68
69 #### declare -i with arithmetic inside strings (Nix, issue 864)
70
71 # example
72 # https://github.com/NixOS/nixpkgs/blob/master/pkgs/stdenv/generic/setup.sh#L379
73
74 declare -i s
75 s='1 + 2'
76 echo s=$s
77
78 declare -a array=(1 2 3)
79 declare -i item
80 item='array[1+1]'
81 echo item=$item
82
83 ## STDOUT:
84 s=3
85 item=3
86 ## END
87 ## N-I osh STDOUT:
88 s=1 + 2
89 item=array[1+1]
90 ## END
91
92 #### append in arith context
93 declare s
94 (( s='1 '))
95 (( s+=' 2 ')) # arith add
96 declare -i i
97 (( i='1 ' ))
98 (( i+=' 2 ' ))
99 declare -i j
100 (( j='x ' )) # treated like zero
101 (( j+=' 2 ' ))
102 echo "$s|$i|$j"
103 ## STDOUT:
104 3|3|2
105 ## END
106
107 #### declare array vs. string: mixing -a +a and () ''
108 # dynamic parsing of first argument.
109 declare +a 'xyz1=1'
110 declare +a 'xyz2=(2 3)'
111 declare -a 'xyz3=4'
112 declare -a 'xyz4=(5 6)'
113 argv.py "${xyz1}" "${xyz2}" "${xyz3[@]}" "${xyz4[@]}"
114 ## STDOUT:
115 ['1', '(2 3)', '4', '5', '6']
116 ## END
117 ## N-I osh STDOUT:
118 ['', '']
119 ## END
120
121
122 #### declare array vs. associative array
123 # Hm I don't understand why the array only has one element. I guess because
124 # index 0 is used twice?
125 declare -a 'array=([a]=b [c]=d)'
126 declare -A 'assoc=([a]=b [c]=d)'
127 argv.py "${#array[@]}" "${!array[@]}" "${array[@]}"
128 argv.py "${#assoc[@]}" "${!assoc[@]}" "${assoc[@]}"
129 ## STDOUT:
130 ['1', '0', 'd']
131 ['2', 'a', 'c', 'b', 'd']
132 ## END
133 ## N-I osh STDOUT:
134 ['0']
135 ['0']
136 ## END
137
138
139 #### declare -l -u
140
141 declare -l lower=FOO
142 declare -u upper=foo
143
144 echo $lower
145 echo $upper
146
147 # other:
148 # -t trace
149 # -I inherit attributes
150
151 ## STDOUT:
152 foo
153 FOO
154 ## END
155
156 ## N-I osh STDOUT:
157 FOO
158 foo
159 ## END