1 ## oils_failures_allowed: 3
2 ## compare_shells: bash dash mksh zsh ash
3
4
5 # POSIX rule about special builtins pointed at:
6 #
7 # https://www.reddit.com/r/oilshell/comments/5ykpi3/oildev_is_alive/
8
9 #### : is special and prefix assignments persist after special builtins (set -o posix)
10 case $SH in
11 dash|zsh)
12 ;;
13 *)
14 # for bash
15 set -o posix
16 ;;
17 esac
18
19 foo=bar :
20 echo foo=$foo
21
22 # Not true when you use 'builtin'
23 z=Z builtin :
24 echo z=$Z
25
26 ## STDOUT:
27 foo=bar
28 z=
29 ## END
30
31 ## BUG zsh STDOUT:
32 foo=
33 z=
34 ## END
35
36 #### readonly is special and prefix assignments persist (set -o posix)
37
38 # Bash only implements it behind the posix option
39 case $SH in
40 dash|zsh)
41 ;;
42 *)
43 # for bash
44 set -o posix
45 ;;
46 esac
47 foo=bar readonly spam=eggs
48 echo foo=$foo
49 echo spam=$spam
50
51 # should NOT be exported
52 printenv.py foo
53 printenv.py spam
54
55 ## STDOUT:
56 foo=bar
57 spam=eggs
58 None
59 None
60 ## END
61
62 ## OK bash/osh STDOUT:
63 foo=bar
64 spam=eggs
65 bar
66 None
67 ## END
68
69 #### true is not special
70 foo=bar true
71 echo $foo
72 ## stdout:
73
74 #### Shift is special and the whole script exits if it returns non-zero
75 if test -n "$BASH_VERSION"; then
76 set -o posix
77 fi
78 set -- a b
79 shift 3
80 echo status=$?
81
82 ## status: 1
83 ## STDOUT:
84 ## END
85
86 ## OK dash status: 2
87
88 ## BUG bash/zsh/ash status: 0
89 ## BUG bash/zsh/ash STDOUT:
90 status=1
91 ## END
92
93 #### set is special and fails, even if using || true
94 shopt -s invalid_ || true
95 echo ok
96 set -o invalid_ || true
97 echo should not get here
98
99 ## status: 1
100 ## STDOUT:
101 ok
102 ## END
103
104 ## OK dash status: 2
105
106 ## BUG bash/ash status: 0
107 ## BUG bash/ash STDOUT:
108 ok
109 should not get here
110 ## END
111
112 #### Special builtins can't be redefined as functions (set -o posix)
113
114 # bash manual says they are 'found before' functions.
115 if test -n "$BASH_VERSION"; then
116 set -o posix
117 fi
118
119 export() {
120 echo 'export func'
121 }
122 export hi
123 echo status=$?
124 ## status: 2
125 ## BUG mksh/zsh status: 0
126 ## BUG mksh/zsh STDOUT:
127 status=0
128 ## END
129
130 #### Non-special builtins CAN be redefined as functions
131 test -n "$BASH_VERSION" && set -o posix
132 true() {
133 echo 'true func'
134 }
135 true hi
136 echo status=$?
137 ## STDOUT:
138 true func
139 status=0
140 ## END
141
142
143 #### command, builtin - both can be redefined, not special (regression)
144 case $SH in dash|ash) exit ;; esac
145
146 builtin echo b
147 command echo c
148
149 builtin() {
150 echo builtin-redef "$@"
151 }
152
153 command() {
154 echo command-redef "$@"
155 }
156
157 builtin echo b
158 command echo c
159
160 ## STDOUT:
161 b
162 c
163 builtin-redef echo b
164 command-redef echo c
165 ## END
166 ## N-I dash/ash STDOUT:
167 ## END