1 ## compare_shells: bash dash mksh zsh
2 ## oils_failures_allowed: 0
3
4 #### can continue after unknown option
5 #
6 # TODO: this is the posix special builtin logic?
7 # dash and mksh make this a fatal error no matter what.
8
9 set -o errexit
10 set -o STRICT || true # unknown option
11 echo hello
12 ## stdout: hello
13 ## status: 0
14 ## BUG dash/mksh/zsh stdout-json: ""
15 ## BUG dash status: 2
16 ## BUG mksh/zsh status: 1
17
18 #### set with both options and argv
19 set -o errexit a b c
20 echo "$@"
21 false
22 echo done
23 ## stdout: a b c
24 ## status: 1
25
26 #### nounset with "$@"
27 set a b c
28 set -u # shouldn't touch argv
29 echo "$@"
30 ## stdout: a b c
31
32 #### set -u -- clears argv
33 set a b c
34 set -u -- # shouldn't touch argv
35 echo "$@"
36 ## stdout:
37
38 #### set -u -- x y z
39 set a b c
40 set -u -- x y z
41 echo "$@"
42 ## stdout: x y z
43
44 #### reset option with long flag
45 set -o errexit
46 set +o errexit
47 echo "[$unset]"
48 ## stdout: []
49 ## status: 0
50
51 #### reset option with short flag
52 set -u
53 set +u
54 echo "[$unset]"
55 ## stdout: []
56 ## status: 0
57
58 #### set -eu (flag parsing)
59 set -eu
60 echo "[$unset]"
61 echo status=$?
62 ## stdout-json: ""
63 ## status: 1
64 ## OK dash status: 2
65
66 #### set -o lists options
67 # NOTE: osh doesn't use the same format yet.
68 set -o | grep -o noexec
69 ## STDOUT:
70 noexec
71 ## END
72
73 #### 'set' and 'eval' round trip
74
75 # NOTE: not testing arrays and associative arrays!
76 _space='[ ]'
77 _whitespace=$'[\t\r\n]'
78 _sq="'single quotes'"
79 _backslash_dq="\\ \""
80 _unicode=$'[\u03bc]'
81
82 # Save the variables
83 varfile=$TMP/vars-$(basename $SH).txt
84
85 set | grep '^_' > "$varfile"
86
87 # Unset variables
88 unset _space _whitespace _sq _backslash_dq _unicode
89 echo [ $_space $_whitespace $_sq $_backslash_dq $_unicode ]
90
91 # Restore them
92
93 . $varfile
94 echo "Code saved to $varfile" 1>&2 # for debugging
95
96 test "$_space" = '[ ]' && echo OK
97 test "$_whitespace" = $'[\t\r\n]' && echo OK
98 test "$_sq" = "'single quotes'" && echo OK
99 test "$_backslash_dq" = "\\ \"" && echo OK
100 test "$_unicode" = $'[\u03bc]' && echo OK
101
102 ## STDOUT:
103 [ ]
104 OK
105 OK
106 OK
107 OK
108 OK
109 ## END
110
111 ## BUG zsh status: 1
112 ## BUG zsh STDOUT:
113 [ ]
114 ## END
115
116 #### set - - and so forth
117 set a b
118 echo "$@"
119
120 set - a b
121 echo "$@"
122
123 set -- a b
124 echo "$@"
125
126 set - -
127 echo "$@"
128
129 set -- --
130 echo "$@"
131
132 # note: zsh is different, and yash is totally different
133 ## STDOUT:
134 a b
135 a b
136 a b
137 -
138 --
139 ## END
140 ## N-I yash STDOUT:
141 a b
142 - a b
143 a b
144 - -
145 --
146 ## END
147 ## BUG zsh STDOUT:
148 a b
149 a b
150 a b
151
152 --
153 ## END
154
155 #### set - leading single dash is ignored, turns off xtrace verbose (#2364)
156
157 show_options() {
158 case $- in
159 *v*) echo verbose-on ;;
160 esac
161 case $- in
162 *x*) echo xtrace-on ;;
163 esac
164 }
165
166 set -x -v
167 show_options
168 echo
169
170 set - a b c
171 echo "$@"
172 show_options
173 echo
174
175 # dash that's not leading is not special
176 set x - y z
177 echo "$@"
178
179 ## STDOUT:
180 verbose-on
181 xtrace-on
182
183 a b c
184
185 x - y z
186 ## END
187
188 ## BUG zsh STDOUT:
189 verbose-on
190 xtrace-on
191
192 a b c
193 verbose-on
194 xtrace-on
195
196 x - y z
197 ## END
198
199 #### set - stops option processing like set --
200 case $SH in zsh) exit ;; esac
201
202 show_options() {
203 case $- in
204 *v*) echo verbose-on ;;
205 esac
206 case $- in
207 *x*) echo xtrace-on ;;
208 esac
209 }
210
211 set -x - -v
212
213 show_options
214 echo argv "$@"
215
216 ## STDOUT:
217 argv -v
218 ## END
219
220 ## N-I zsh STDOUT:
221 ## END
222
223 #### A single + is an ignored flag; not an argument
224 case $SH in zsh) exit ;; esac
225
226 show_options() {
227 case $- in
228 *v*) echo verbose-on ;;
229 esac
230 case $- in
231 *x*) echo xtrace-on ;;
232 esac
233 }
234
235 set +
236 echo plus "$@"
237
238 set -x + -v x y
239 show_options
240 echo plus "$@"
241
242 ## STDOUT:
243 plus
244 verbose-on
245 xtrace-on
246 plus x y
247 ## END
248
249 ## BUG mksh STDOUT:
250 plus
251 xtrace-on
252 plus -v x y
253 ## END
254
255 ## N-I zsh STDOUT:
256 ## END
257
258 #### set - + and + -
259 set - +
260 echo "$@"
261
262 set + -
263 echo "$@"
264
265 ## STDOUT:
266 +
267 +
268 ## END
269
270 ## BUG mksh STDOUT:
271 +
272 -
273 ## END
274
275 ## OK zsh/osh STDOUT:
276 +
277
278 ## END