OILS / doc / ref / chap-option.md View on Github | oils.pub

352 lines, 226 significant
1---
2title: Global Shell Options (Oils Reference)
3all_docs_url: ..
4body_css_class: width40
5default_highlighter: oils-sh
6preserve_anchor_case: yes
7---
8
9<div class="doc-ref-header">
10
11[Oils Reference](index.html) &mdash;
12Chapter **Global Shell Options**
13
14</div>
15
16This chapter describes global shell options in Oils. Some options are from
17POSIX shell, and some are from [bash]($xref). We also use options to turn
18[OSH]($xref) into [YSH]($xref).
19
20<span class="in-progress">(in progress)</span>
21
22<div id="dense-toc">
23</div>
24
25## Errors
26
27These options are from POSIX shell:
28
29 nounset -u
30 errexit -e
31
32These are from bash:
33
34 inherit_errexit:
35 pipefail
36
37## Globbing
38
39These options are from POSIX shell:
40
41 noglob -f
42
43From bash:
44
45 nullglob failglob dotglob
46
47From Oils:
48
49 dashglob
50
51Some details:
52
53### nullglob
54
55When `nullglob` is on, a glob matching no files expands to no arguments:
56
57 shopt -s nullglob
58 $ echo L *.py R
59 L R
60
61Without this option, the glob string itself is returned:
62
63 $ echo L *.py R # no Python files in this dir
64 L *.py R
65
66(This option is from GNU bash.)
67
68### dashglob
69
70Do globs return results that start with `-`? It's on by default in `bin/osh`,
71but off when YSH is enabled.
72
73Turning it off prevents a command like `rm *` from being confused by a file
74called `-rf`.
75
76 $ touch -- myfile -rf
77
78 $ echo *
79 -rf myfile
80
81 $ shopt -u dashglob
82 $ echo *
83 myfile
84
85## Other Option
86
87 noclobber -C # Redirects can't overwrite files
88
89## Debugging
90
91<h3 id="errtrace">errtrace (-E)</h3>
92
93Enable the ERR [trap][] in both shell functions and subshells.
94
95The option is also `set -E`. It's designed to be compatible with bash.
96
97[trap]: chap-builtin-cmd.html#trap
98
99### extdebug
100
101Show more info in when printing functions with `declare -f`. Used by
102`task-five.sh`.
103
104<h3 id="xtrace">xtrace (-x)</h3>
105
106Show execution traces.
107
108- In OSH, the [PS4][] variables control the display.
109- In YSH, the `SHX_*` variables control the display.
110
111[PS4]: chap-plugin.html#PS4
112
113This option is also `set -x`. It's required by POSIX shell.
114
115### verbose
116
117Not implemented.
118
119This option is from POSIX shell.
120
121## Interactive
122
123These options are from bash.
124
125 emacs vi
126
127
128## Compat
129
130### eval_unsafe_arith
131
132Allow dynamically parsed `a[$(echo 42)]` For bash compatibility.
133
134### ignore_flags_not_impl
135
136Suppress failures from unimplemented flags. Example:
137
138 shopt --set ignore_flags_not_impl
139
140 declare -i foo=2+3 # not evaluated to 5, but doesn't fail either
141
142This option can be useful for "getting past" errors while testing.
143
144### ignore_shopt_not_impl
145
146Suppress failures from unimplemented shell options. Example:
147
148 shopt --set ignore_shopt_not_impl
149
150 shopt --set xpg_echo # exit with status 0, not 1
151 # this is a bash option that OSH doesn't implement
152
153This option can be useful for "getting past" errors while testing.
154
155## Groups
156
157To turn OSH into YSH, we use three option groups. Some of them allow new
158features, and some disallow old features.
159
160<!-- note: explicit anchor necessary because of mangling -->
161<h3 id="strict:all">strict:all</h3>
162
163Option in this group disallow problematic or confusing shell constructs. The
164resulting script will still run in another shell.
165
166 shopt --set strict:all # turn on all options
167 shopt -p strict:all # print their current state
168
169Parsing options:
170
171 strict_parse_slice # No implicit length for ${a[@]::}
172 X strict_parse_utf8 # Source code must be valid UTF-8
173
174Runtime options:
175
176 strict_argv # No empty argv
177 strict_arith # Fatal parse errors (on by default)
178 strict_array # Arrays and strings aren't confused
179 strict_control_flow # Disallow misplaced keyword, empty arg
180 strict_errexit # Disallow code that ignores failure
181 strict_nameref # Trap invalid variable names
182 strict_word_eval # Expose unicode and slicing errors
183 strict_tilde # Tilde subst can result in error
184 X strict_glob # Parse the sublanguage more strictly
185
186<h3 id="ysh:upgrade">ysh:upgrade</h3>
187
188Options in this group enable new YSH features. It doesn't break existing shell
189scripts when it's avoidable.
190
191For example, `parse_at` means that `@myarray` is now the operation to splice
192an array. This will break scripts that expect `@` to be literal, but you can
193simply quote it like `'@literal'` to fix the problem.
194
195 shopt --set ysh:upgrade # turn on all options
196 shopt -p ysh:upgrade # print their current state
197
198Details on each option:
199
200 parse_at echo @array @[arrayfunc(x, y)]
201 parse_brace if true { ... }; cd ~/src { ... }
202 parse_equals x = 'val' in Caps { } config blocks
203 parse_paren if (x > 0) ...
204 parse_proc proc p { ... }
205 parse_triple_quote """$x""" '''x''' (command mode)
206 parse_ysh_string echo r'\' u'\\' b'\\' (command mode)
207 command_sub_errexit Synchronous errexit check
208 process_sub_fail Analogous to pipefail for process subs
209 sigpipe_status_ok status 141 -> 0 in pipelines
210 simple_word_eval No splitting, static globbing
211 xtrace_rich Hierarchical and process tracing
212 xtrace_details (-u) Disable most tracing with +
213 dashglob (-u) Disabled to avoid files like -rf
214 env_obj Init ENV Obj at startup; use it when starting
215 child processes
216 for_loop_frames YSH can create closures from loop vars
217
218<h3 id="ysh:all">ysh:all</h3>
219
220Enable the full YSH language. This includes everything in the `ysh:upgrade`
221group and the `strict:all` group.
222
223 shopt --set ysh:all # turn on all options
224 shopt -p ysh:all # print their current state
225
226Details on options that are not in `ysh:upgrade` and `strict:all`:
227
228 parse_at_all @ starting any word is an operator
229 parse_backslash (-u) Allow bad backslashes in "" and $''
230 parse_backticks (-u) Allow legacy syntax `echo hi`
231 parse_bare_word (-u) 'case unquoted' and 'for x in unquoted'
232 parse_dollar (-u) Allow bare $ to mean \$ (maybe $/d+/)
233 parse_dbracket (-u) Is legacy [[ allowed?
234 parse_dparen (-u) Is (( legacy arithmetic allowed?
235 parse_ignored (-u) Parse, but ignore, certain redirects
236 parse_sh_arith (-u) Allow legacy shell arithmetic
237 expand_aliases (-u) Whether aliases are expanded
238 X old_builtins (-u) local/declare/etc. pushd/popd/dirs
239 ... source unset printf [un]alias
240 ... getopts
241 X old_syntax (-u) ( ) ${x%prefix} ${a[@]} $$
242 no_exported Environ doesn't correspond to exported (-x) vars
243 no_init_globals At startup, don't set vars like PWD, SHELLOPTS
244 simple_echo echo doesn't accept flags -e -n
245 simple_eval_builtin eval takes exactly 1 argument
246 simple_test_builtin 3 args or fewer; use test not [
247 X simple_trap Function name only
248 verbose_errexit Whether to print detailed errors
249
250## YSH Details
251
252### opts-redefine
253
254In the interactive shell, you can redefine procs and funcs.
255
256 redefine_source 'source-guard' builtin always returns 0
257 X redefine_const Can consts be redefined?
258
259### opts-internal
260
261These options are used by the interpreter. You generally shouldn't set them
262yourself.
263
264 _allow_command_sub To implement strict_errexit, eval_unsafe_arith
265 _allow_process_sub To implement strict_errexit
266 dynamic_scope To implement proc and func
267 _no_debug_trap Used in pipelines in job control shell
268 _running_trap To disable strict_errexit
269 _running_hay Hay evaluation
270
271## Unlinked Descriptions
272
273Here are some descriptions of individual options.
274
275### strict_control_flow
276
277Disallow `break` and `continue` at the top level, and disallow empty args like
278`return $empty`.
279
280### strict_tilde
281
282Failed tilde expansions cause hard errors (like zsh) rather than silently
283evaluating to `~` or `~bad`.
284
285
286### strict_nameref
287
288When `strict_nameref` is set, undefined references produce fatal errors:
289
290 declare -n ref
291 echo $ref # fatal error, not empty string
292 ref=x # fatal error instead of decaying to non-reference
293
294References that don't contain variables also produce hard errors:
295
296 declare -n ref='not a var'
297 echo $ref # fatal
298 ref=x # fatal
299
300### parse_ignored
301
302For compatibility, YSH will parse some constructs it doesn't execute, like:
303
304 return 0 2>&1 # redirect on control flow
305
306When this option is disabled, that statement is a syntax error.
307
308### parse_triple_quote
309
310Parse the shell-style multi-line strings, which strip leading whitespace:
311
312 echo '''
313 one
314 two
315 '''
316
317 echo """
318 hello
319 $name
320 """
321
322(This option affects only command mode. Such strings are always parsed in
323expression mode.)
324
325### parse_ysh_string
326
327Allow `r'\'` and `u'\\'` and `b'\\'` strings, as well as their multi-line
328versions.
329
330Since shell strings are already raw, this means that YSH just ignores the r
331prefix:
332
333 echo r'\' # a single backslash
334
335J8 unicode strings:
336
337 echo u'mu \u{3bc}' # mu char
338
339J8 byte strings:
340
341 echo b'byte \yff'
342
343(This option affects only command mode. Such strings are always parsed in
344expression mode.)
345
346### sigpipe_status_ok
347
348If a process that's part of a pipeline exits with status 141 when this is
349option is on, it's turned into status 0, which avoids failure.
350
351SIGPIPE errors occur in cases like 'yes | head', and generally aren't useful.
352