1 |
#!/usr/bin/env bash |
2 |
# |
3 |
# POSIX rule about special builtins pointed at: |
4 |
# |
5 |
# https://www.reddit.com/r/oilshell/comments/5ykpi3/oildev_is_alive/ |
6 |
|
7 |
#### : is special and prefix assignments persist after special builtins |
8 |
# Bash only implements it behind the posix option |
9 |
test -n "$BASH_VERSION" && set -o posix |
10 |
foo=bar : |
11 |
echo $foo |
12 |
## stdout: bar |
13 |
|
14 |
#### true is not special |
15 |
foo=bar true |
16 |
echo $foo |
17 |
## stdout: |
18 |
|
19 |
#### Shift is special and the whole script exits if it returns non-zero |
20 |
test -n "$BASH_VERSION" && set -o posix |
21 |
set -- a b |
22 |
shift 3 |
23 |
echo status=$? |
24 |
## stdout-json: "" |
25 |
## status: 1 |
26 |
## OK dash status: 2 |
27 |
## BUG bash status: 0 |
28 |
## BUG bash STDOUT: |
29 |
status=1 |
30 |
## END |
31 |
|
32 |
#### set is special and fails, even if using || true |
33 |
shopt -s invalid_ || true |
34 |
echo ok |
35 |
set -o invalid_ || true |
36 |
echo should not get here |
37 |
## STDOUT: |
38 |
ok |
39 |
## END |
40 |
## status: 1 |
41 |
## OK dash status: 2 |
42 |
## BUG bash status: 0 |
43 |
## BUG bash STDOUT: |
44 |
ok |
45 |
should not get here |
46 |
## END |
47 |
|
48 |
#### Special builtins can't be redefined as functions |
49 |
# bash manual says they are 'found before' functions. |
50 |
test -n "$BASH_VERSION" && set -o posix |
51 |
export() { |
52 |
echo 'export func' |
53 |
} |
54 |
export hi |
55 |
echo status=$? |
56 |
## status: 2 |
57 |
## BUG mksh status: 0 |
58 |
## BUG mksh stdout: status=0 |
59 |
|
60 |
#### Non-special builtins CAN be redefined as functions |
61 |
test -n "$BASH_VERSION" && set -o posix |
62 |
true() { |
63 |
echo 'true func' |
64 |
} |
65 |
true hi |
66 |
echo status=$? |
67 |
## STDOUT: |
68 |
true func |
69 |
status=0 |
70 |
## END |