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 #### declare -i with +=
12 declare s
13 s='1 '
14 s+=' 2 ' # string append
15
16 declare -i i
17 i='1 '
18 i+=' 2 ' # arith add
19
20 declare -i j
21 j=x # treated like zero
22 j+=' 2 ' # arith add
23
24 echo "[$s]"
25 echo [$i]
26 echo [$j]
27 ## STDOUT:
28 [1 2 ]
29 [3]
30 [2]
31 ## END
32 ## N-I osh STDOUT:
33 [1 2 ]
34 [1 2 ]
35 [x 2 ]
36 ## END
37
38 #### declare -i with arithmetic inside strings (Nix, issue 864)
39
40 # example
41 # https://github.com/NixOS/nixpkgs/blob/master/pkgs/stdenv/generic/setup.sh#L379
42
43 declare -i s
44 s='1 + 2'
45 echo s=$s
46
47 declare -a array=(1 2 3)
48 declare -i item
49 item='array[1+1]'
50 echo item=$item
51
52 ## STDOUT:
53 s=3
54 item=3
55 ## END
56 ## N-I osh STDOUT:
57 s=1 + 2
58 item=array[1+1]
59 ## END
60
61 #### append in arith context
62 declare s
63 (( s='1 '))
64 (( s+=' 2 ')) # arith add
65 declare -i i
66 (( i='1 ' ))
67 (( i+=' 2 ' ))
68 declare -i j
69 (( j='x ' )) # treated like zero
70 (( j+=' 2 ' ))
71 echo "$s|$i|$j"
72 ## STDOUT:
73 3|3|2
74 ## END
75
76 #### declare array vs. string: mixing -a +a and () ''
77 # dynamic parsing of first argument.
78 declare +a 'xyz1=1'
79 declare +a 'xyz2=(2 3)'
80 declare -a 'xyz3=4'
81 declare -a 'xyz4=(5 6)'
82 argv.py "${xyz1}" "${xyz2}" "${xyz3[@]}" "${xyz4[@]}"
83 ## STDOUT:
84 ['1', '(2 3)', '4', '5', '6']
85 ## END
86 ## N-I osh STDOUT:
87 ['', '']
88 ## END
89
90
91 #### declare array vs. associative array
92 # Hm I don't understand why the array only has one element. I guess because
93 # index 0 is used twice?
94 declare -a 'array=([a]=b [c]=d)'
95 declare -A 'assoc=([a]=b [c]=d)'
96 argv.py "${#array[@]}" "${!array[@]}" "${array[@]}"
97 argv.py "${#assoc[@]}" "${!assoc[@]}" "${assoc[@]}"
98 ## STDOUT:
99 ['1', '0', 'd']
100 ['2', 'a', 'c', 'b', 'd']
101 ## END
102 ## N-I osh STDOUT:
103 ['0']
104 ['0']
105 ## END