1 ## oils_failures_allowed: 3
2 ## compare_shells: bash dash mksh zsh ash
3
4 # POSIX rule about special builtins pointed at:
5 #
6 # https://www.reddit.com/r/oilshell/comments/5ykpi3/oildev_is_alive/
7
8 #### Prefix assignments persist after special builtins, like : (set -o posix)
9 case $SH in
10 bash)
11 set -o posix
12 ;;
13 esac
14
15 foo=bar :
16 echo foo=$foo
17
18 # Not true when you use 'builtin'
19 z=Z builtin :
20 echo z=$Z
21
22 ## STDOUT:
23 foo=bar
24 z=
25 ## END
26
27 ## BUG zsh STDOUT:
28 foo=
29 z=
30 ## END
31
32 #### readonly is special and prefix assignments persist (set -o posix)
33
34 # Bash only implements it behind the posix option
35 case $SH in
36 bash)
37 # for bash
38 set -o posix
39 ;;
40 esac
41 foo=bar readonly spam=eggs
42 echo foo=$foo
43 echo spam=$spam
44
45 # should NOT be exported
46 printenv.py foo
47 printenv.py spam
48
49 ## STDOUT:
50 foo=bar
51 spam=eggs
52 None
53 None
54 ## END
55
56 ## OK bash/osh STDOUT:
57 foo=bar
58 spam=eggs
59 bar
60 None
61 ## END
62
63 #### Special builtins can't be redefined as shell functions : (set -o posix)
64 case $SH in
65 bash)
66 set -o posix
67 ;;
68 esac
69
70 eval 'echo hi'
71
72 eval() {
73 echo 'sh func' "$@"
74 }
75
76 eval 'echo hi'
77
78 ## status: 2
79 ## STDOUT:
80 hi
81 ## END
82
83 ## BUG mksh status: 0
84 ## BUG mksh STDOUT:
85 hi
86 hi
87 ## END
88
89 ## BUG zsh status: 0
90 ## BUG zsh STDOUT:
91 hi
92 sh func echo hi
93 ## END
94
95 #### Non-special builtins CAN be redefined as functions
96 test -n "$BASH_VERSION" && set -o posix
97 true() {
98 echo 'true func'
99 }
100 true hi
101 echo status=$?
102 ## STDOUT:
103 true func
104 status=0
105 ## END
106
107 #### true is not special; prefix assignments don't persist
108 foo=bar true
109 echo $foo
110 ## stdout:
111
112 #### Shift is special and the whole script exits if it returns non-zero
113 $SH -c '
114 if test -n "$BASH_VERSION"; then
115 set -o posix
116 fi
117 set -- a b
118 shift 3
119 echo status=$?
120 '
121 if test "$?" != 0; then
122 echo 'non-zero status'
123 fi
124
125 ## STDOUT:
126 non-zero status
127 ## END
128
129 ## BUG bash/zsh/ash status: 0
130 ## BUG bash/zsh/ash STDOUT:
131 status=1
132 ## END
133
134 #### set is special and fails, even if using || true
135 $SH -c '
136 shopt -s invalid_ || true
137 echo ok
138 set -o invalid_ || true
139 echo should not get here
140 '
141 if test "$?" != 0; then
142 echo 'non-zero status'
143 fi
144
145 ## STDOUT:
146 ok
147 non-zero status
148 ## END
149
150 ## BUG bash/ash status: 0
151 ## BUG bash/ash STDOUT:
152 ok
153 should not get here
154 ## END
155
156 #### bash 'type' gets confused - says 'function', but runs builtin
157 case $SH in dash|mksh|zsh|ash) exit ;; esac
158
159 echo TRUE
160 type -t true # builtin
161 true() { echo true func; }
162 type -t true # now a function
163 echo ---
164
165 echo EVAL
166
167 type -t eval # builtin
168 # define function before set -o posix
169 eval() { echo "$1"; }
170 # bash runs the FUNCTION, but OSH finds the special builtin
171 # OSH doesn't need set -o posix
172 eval 'echo before posix'
173
174 if test -n "$BASH_VERSION"; then
175 # this makes the eval definition invisible!
176 set -o posix
177 fi
178
179 eval 'echo after posix' # this is the builtin eval
180 # it claims it's a function, but it's a builtin
181 type -t eval
182
183 # it finds the function and the special builtin
184 #type -a eval
185
186 ## OK bash STDOUT:
187 TRUE
188 builtin
189 function
190 ---
191 EVAL
192 builtin
193 echo before posix
194 after posix
195 function
196 ## END
197
198 ## OK osh STDOUT:
199 TRUE
200 builtin
201 function
202 ---
203 EVAL
204 builtin
205 before posix
206 after posix
207 function
208 ## END
209
210 ## N-I dash/mksh/zsh/ash STDOUT:
211 ## END
212
213 #### command, builtin - both can be redefined, not special (regression)
214 case $SH in dash|ash) exit ;; esac
215
216 builtin echo b
217 command echo c
218
219 builtin() {
220 echo builtin-redef "$@"
221 }
222
223 command() {
224 echo command-redef "$@"
225 }
226
227 builtin echo b
228 command echo c
229
230 ## STDOUT:
231 b
232 c
233 builtin-redef echo b
234 command-redef echo c
235 ## END
236 ## N-I dash/ash STDOUT:
237 ## END