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

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