1 ## compare_shells: bash
2 ## oils_failures_allowed: 2
3
4 #### var ref to array 'preHooks[@]'
5 #
6 # This idiom discussed on
7 # https://github.com/NixOS/nixpkgs/pull/147629
8
9 show() {
10 echo show
11
12 # These are actually different
13 argv.py ${!hooksSlice}
14
15 argv.py ${!hooksSlice+"${!hooksSlice}"}
16 }
17
18 hooksSlice='preHooks[@]'
19
20 preHooks=()
21 show
22
23 preHooks=('foo bar' baz)
24 show
25
26 # WTF this exposes a difference? But not the test case below?
27
28 # What's happening here?
29 # Uncomment this and get an error in bash about hookSlice, even though we never
30 # undefined it.
31
32 #wtf=1
33 #
34 # line 6: !hooksSlice: unbound variable
35
36 if test -n "$wtf"; then
37 # 4.4.0(1)-release
38 # echo $BASH_VERSION
39
40 set -u
41 preHooks=()
42 show
43
44 preHooks=('foo bar' baz)
45 show
46 fi
47
48 ## STDOUT:
49 show
50 []
51 []
52 show
53 ['foo', 'bar', 'baz']
54 ['foo bar', 'baz']
55 ## END
56
57 #### Same as above with set -u
58 show() {
59 echo show
60
61 # bash gives an error here - !hookSlice unbound, even though preHooks exists
62 # OSH currently does the "logical" thing
63 argv.py ${!hooksSlice}
64
65 argv.py ${!hooksSlice+"${!hooksSlice}"}
66 }
67
68 hooksSlice='preHooks[@]'
69
70 set -u
71 preHooks=()
72 show
73
74 preHooks=('foo bar' baz)
75 show
76
77 ## STDOUT:
78 show
79 ## END
80 ## status: 1
81
82
83 #### ${!ref} to undefined array
84
85 set -u
86 shopt -s eval_unsafe_arith || true 2>/dev/null
87
88 #preHooks=()
89 hookSlice="preHooks[@]"
90
91 argv.py ${!hookSlice+"${!hookSlice}"}
92
93 for element in ${!hookSlice+"${!hookSlice}"}; do
94 echo $element
95 done
96
97 ## STDOUT:
98 []
99 ## END
100
101 #### ${!ref} to undefined string var is fatal, INCONSISTENT with array
102 hookSlice='preHooks'
103
104 argv.py ${!hookSlice}
105
106 set -u
107
108 argv.py ${!hookSlice}
109
110 echo end
111
112 ## status: 1
113 ## STDOUT:
114 []
115 ## END
116
117 #### export with dynamic var name +=
118
119 orig() {
120 export NIX_LDFLAGS${role_post}+=" -L$1/lib64"
121 }
122
123 new() {
124 local var_name="NIX_LDFLAGS$role_post"
125 local value=" -L$1/lib64"
126
127 eval "$var_name"+='$value'
128 export "$var_name"
129 }
130
131 role_post='_foo'
132
133 # set -u
134
135 if test -n "${BASH_VERSION:-}"; then
136 orig one
137 fi
138
139 declare -p NIX_LDFLAGS_foo # inspect it
140 unset NIX_LDFLAGS_foo
141
142 new one
143
144 declare -p NIX_LDFLAGS_foo # inspect it
145
146 ## STDOUT:
147 declare -x NIX_LDFLAGS_foo=" -Lone/lib64"
148 declare -x NIX_LDFLAGS_foo=" -Lone/lib64"
149 ## END
150 ## OK osh STDOUT:
151 declare -x NIX_LDFLAGS_foo=' -Lone/lib64'
152 ## END
153