1 #
2 # Tests for bash's type flags on cells. Hopefully we don't have to implement
3 # this, but it's good to know the behavior.
4 #
5 # OSH follows a Python-ish model of types carried with values/objects, not
6 # locations.
7 #
8 # See https://github.com/oilshell/oil/issues/26
9
10 #### declare -i
11 declare s
12 s='1 '
13 s+=' 2 ' # string append
14 declare -i i
15 i='1 '
16 i+=' 2 ' # arith add
17 declare -i j
18 j=x # treated like zero
19 j+=' 2 ' # arith add
20 echo "$s|$i|$j"
21 ## stdout: 1 2 |3|2
22
23 #### append in arith context
24 declare s
25 (( s='1 '))
26 (( s+=' 2 ')) # arith add
27 declare -i i
28 (( i='1 ' ))
29 (( i+=' 2 ' ))
30 declare -i j
31 (( j='x ' )) # treated like zero
32 (( j+=' 2 ' ))
33 echo "$s|$i|$j"
34 ## stdout: 3|3|2
35
36 #### declare array vs. string: mixing -a +a and () ''
37 # dynamic parsing of first argument.
38 declare +a 'xyz1=1'
39 declare +a 'xyz2=(2 3)'
40 declare -a 'xyz3=4'
41 declare -a 'xyz4=(5 6)'
42 argv.py "${xyz1}" "${xyz2}" "${xyz3[@]}" "${xyz4[@]}"
43 ## stdout: ['1', '(2 3)', '4', '5', '6']
44
45 #### declare array vs. associative array
46 # Hm I don't understand why the array only has one element. I guess because
47 # index 0 is used twice?
48 declare -a 'array=([a]=b [c]=d)'
49 declare -A 'assoc=([a]=b [c]=d)'
50 argv.py "${#array[@]}" "${!array[@]}" "${array[@]}"
51 argv.py "${#assoc[@]}" "${!assoc[@]}" "${assoc[@]}"
52 ## stdout-json: "['1', '0', 'd']\n['2', 'a', 'c', 'b', 'd']\n"