OILS / spec / sh-options-bash.test.sh View on Github | oils.pub

153 lines, 65 significant
1## compare_shells: bash
2## oils_failures_allowed: 2
3
4#### SHELLOPTS is updated when options are changed
5echo $SHELLOPTS | grep -q xtrace
6echo $?
7set -x
8echo $SHELLOPTS | grep -q xtrace
9echo $?
10set +x
11echo $SHELLOPTS | grep -q xtrace
12echo $?
13## STDOUT:
141
150
161
17## END
18## N-I dash/mksh STDOUT:
191
201
211
22## END
23
24#### SHELLOPTS is readonly
25SHELLOPTS=x
26echo status=$?
27## stdout: status=1
28## N-I dash/mksh stdout: status=0
29
30# Setting a readonly variable in osh is a hard failure.
31## OK osh status: 1
32## OK osh stdout-json: ""
33
34#### SHELLOPTS and BASHOPTS are non-empty
35
36# 2024-06 - tickled by Samuel testing Gentoo
37
38if test -v SHELLOPTS; then
39 echo 'shellopts is set'
40fi
41if test -v BASHOPTS; then
42 echo 'bashopts is set'
43fi
44
45# bash: braceexpand:hashall etc.
46
47echo shellopts ${SHELLOPTS:?} > /dev/null
48echo bashopts ${BASHOPTS:?} > /dev/null
49
50## STDOUT:
51shellopts is set
52bashopts is set
53## END
54
55## N-I dash status: 2
56## N-I mksh status: 1
57
58#### SHELLOPTS reflects flags like sh -x
59
60$SH -x -c 'echo $SHELLOPTS' | grep -o xtrace
61
62## STDOUT:
63xtrace
64## END
65
66#### export SHELLOPTS does cross-process tracing
67
68$SH -c '
69export SHELLOPTS
70set -x
71echo 1
72$SH -c "echo 2"
73' 2>&1 | sed 's/.*sh /sh /g'
74
75## STDOUT:
76+ echo 1
771
78sh -c 'echo 2'
79+ echo 2
802
81## END
82
83#### export SHELLOPTS does cross-process tracing with bash
84
85# calling bash
86$SH -c '
87export SHELLOPTS
88set -x
89#echo SHELLOPTS=$SHELLOPTS
90echo 1
91bash -c "echo 2"
92' 2>&1 | sed 's/.*sh /sh /g'
93
94## STDOUT:
95+ echo 1
961
97sh -c 'echo 2'
98+ echo 2
992
100## END
101
102#### OSH calling bash with SHELLOPTS does not change braceexpand
103
104#echo outside=$SHELLOPTS
105
106# sed pattern to normalize spaces
107normalize='s/[ \t]\+/ /g'
108
109bash -c '
110#echo bash=$SHELLOPTS
111set -o | grep braceexpand | sed "$1"
112' unused "$normalize"
113
114env SHELLOPTS= bash -c '
115#echo bash2=$SHELLOPTS
116set -o | grep braceexpand | sed "$1"
117' unused "$normalize"
118
119## STDOUT:
120braceexpand on
121braceexpand on
122## END
123
124#### If shopt --set xtrace is allowed, it should update SHELLOPTS, not BASHOPTS
125case $SH in bash) exit ;; esac
126
127shopt --set xtrace
128echo SHELLOPTS=$SHELLOPTS
129set -x
130echo SHELLOPTS=$SHELLOPTS
131set +x
132echo SHELLOPTS=$SHELLOPTS
133
134## STDOUT:
135SHELLOPTS=xtrace
136SHELLOPTS=xtrace
137SHELLOPTS=
138## END
139## N-I bash STDOUT:
140## END
141
142#### shopt -s progcomp hostcomplete are stubs (bash-completion)
143
144shopt -s progcomp hostcomplete
145echo status=$?
146
147shopt -u progcomp hostcomplete
148echo status=$?
149
150## STDOUT:
151status=0
152status=0
153## END