OILS / spec / builtin-special.test.sh View on Github | oilshell.org

114 lines, 59 significant
1## oils_failures_allowed: 4
2## compare_shells: bash dash mksh zsh
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
10case $SH in
11 dash|zsh|*osh)
12 ;;
13 *)
14 # for bash
15 set -o posix
16 ;;
17esac
18foo=bar :
19echo foo=$foo
20## STDOUT:
21foo=bar
22## END
23## BUG zsh STDOUT:
24foo=
25## END
26
27#### readonly is special and prefix assignments persist
28
29# Bash only implements it behind the posix option
30case $SH in
31 dash|zsh|*osh)
32 ;;
33 *)
34 # for bash
35 set -o posix
36 ;;
37esac
38foo=bar readonly spam=eggs
39echo foo=$foo
40echo spam=$spam
41
42# should NOT be exported
43printenv.py foo
44printenv.py spam
45## STDOUT:
46foo=bar
47spam=eggs
48None
49None
50## END
51## BUG bash STDOUT:
52foo=bar
53spam=eggs
54bar
55None
56## END
57
58#### true is not special
59foo=bar true
60echo $foo
61## stdout:
62
63#### Shift is special and the whole script exits if it returns non-zero
64test -n "$BASH_VERSION" && set -o posix
65set -- a b
66shift 3
67echo status=$?
68## stdout-json: ""
69## status: 1
70## OK dash status: 2
71## BUG bash/zsh status: 0
72## BUG bash/zsh STDOUT:
73status=1
74## END
75
76#### set is special and fails, even if using || true
77shopt -s invalid_ || true
78echo ok
79set -o invalid_ || true
80echo should not get here
81## STDOUT:
82ok
83## END
84## status: 1
85## OK dash status: 2
86## BUG bash status: 0
87## BUG bash STDOUT:
88ok
89should not get here
90## END
91
92#### Special builtins can't be redefined as functions
93# bash manual says they are 'found before' functions.
94test -n "$BASH_VERSION" && set -o posix
95export() {
96 echo 'export func'
97}
98export hi
99echo status=$?
100## status: 2
101## BUG mksh/zsh status: 0
102## BUG mksh/zsh stdout: status=0
103
104#### Non-special builtins CAN be redefined as functions
105test -n "$BASH_VERSION" && set -o posix
106true() {
107 echo 'true func'
108}
109true hi
110echo status=$?
111## STDOUT:
112true func
113status=0
114## END