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

408 lines, 263 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 pipefail
32
33These are from bash:
34
35 inherit_errexit
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 no_dash_glob
50
51Some details:
52
53### dotglob
54
55Does glob expansion return files that start with a period?
56
57 # By default, we don't return say .gitignore
58 $ echo * # foo
59
60 $ shopt -s dotglob # set this option
61 $ echo * # => .gitignore foo
62
63This option is on by default in OSH and YSH.
64
65### nullglob
66
67When `nullglob` is on, a glob matching no files expands to no arguments:
68
69 shopt -s nullglob
70 $ echo L *.py R
71 L R
72
73Without this option, the glob string itself is returned:
74
75 $ echo L *.py R # no Python files in this dir
76 L *.py R
77
78(This option is from GNU bash.)
79
80YSH doesn't use this option, but its [Simple Word
81Evaluation](../simple-word-eval.html) algorithm behaves similarly.
82
83### no_dash_glob
84
85Do globs return results that start with `-`? It's on by default in `bin/osh`,
86but off when YSH is enabled.
87
88Turning it off prevents a command like `rm *` from being confused by a file
89called `-rf`.
90
91 $ touch -- myfile -rf
92
93 $ echo *
94 -rf myfile
95
96 $ shopt -s no_dash_glob
97 $ echo *
98 myfile
99
100This option is off in OSH, and on in YSH.
101
102## Other Option
103
104 noclobber -C # Redirects can't overwrite files
105
106## Debugging
107
108<h3 id="errtrace">errtrace (-E)</h3>
109
110Enable the ERR [trap][] in both shell functions and subshells.
111
112The option is also `set -E`. It's designed to be compatible with bash.
113
114[trap]: chap-builtin-cmd.html#trap
115
116### extdebug
117
118Show more info in when printing functions with `declare -f`. Used by
119`task-five.sh`.
120
121<h3 id="xtrace">xtrace (-x)</h3>
122
123Show execution traces.
124
125- In OSH, the [PS4][] variables control the display.
126- In YSH, the `SHX_*` variables control the display.
127
128[PS4]: chap-plugin.html#PS4
129
130This option is also `set -x`. It's required by POSIX shell.
131
132### verbose
133
134Not implemented.
135
136This option is from POSIX shell.
137
138## Interactive
139
140These options are from bash.
141
142 emacs vi
143
144
145## Compat
146
147### eval_unsafe_arith
148
149Allow dynamically parsed `a[$(echo 42)]` For bash compatibility.
150
151### ignore_flags_not_impl
152
153Suppress failures from unimplemented flags. Example:
154
155 shopt --set ignore_flags_not_impl
156
157 declare -i foo=2+3 # not evaluated to 5, but doesn't fail either
158
159This option can be useful for "getting past" errors while testing.
160
161### ignore_shopt_not_impl
162
163Suppress failures from unimplemented shell options. Example:
164
165 shopt --set ignore_shopt_not_impl
166
167 shopt --set xpg_echo # exit with status 0, not 1
168 # this is a bash option that OSH doesn't implement
169
170This option can be useful for "getting past" errors while testing.
171
172## Optimize
173
174### rewrite_extern
175
176This options enables a transparent rewriting of external commands to
177**builtins**.
178
179Currently, these commands **may** be rewritten, depending on their `argv`:
180
181- [cat][]
182- [rm][]
183
184These optimizations are *sound* - they should not affect the behavior of
185programs on POSIX system.
186
187---
188
189This option is on by default in OSH and YSH, but it applies only in
190non-interactive shells. That is, in interactive shells, commands are **never**
191rewritten, regardless of the value of `rewrite_extern`.
192
193[cat]: chap-builtin-cmd.html#cat
194[rm]: chap-builtin-cmd.html#rm
195
196## Groups
197
198To turn OSH into YSH, we use three option groups. Some of them allow new
199features, and some disallow old features.
200
201<!-- note: explicit anchor necessary because of mangling -->
202<h3 id="strict:all">strict:all</h3>
203
204Option in this group disallow problematic or confusing shell constructs. The
205resulting script will still run in another shell.
206
207 shopt --set strict:all # turn on all options
208 shopt -p strict:all # print their current state
209
210Parsing options:
211
212 strict_parse_equals # Disallow '=x' to avoid confusion with '= x'
213 strict_parse_slice # No implicit length for ${a[@]::}
214 X strict_parse_utf8 # Source code must be valid UTF-8
215
216Runtime options:
217
218 strict_arg_parse # Disallow additional arguments to some builtins
219 strict_argv # No empty argv
220 strict_arith # Fatal parse errors (on by default)
221 strict_array # Arrays and strings aren't confused
222 strict_control_flow # Disallow misplaced keyword, empty arg
223 strict_env_binding # Prefix bindings must always be env bindings
224 strict_errexit # Disallow code that ignores failure
225 strict_nameref # Trap invalid variable names
226 strict_word_eval # Expose unicode and slicing errors
227 strict_tilde # Tilde subst can result in error
228 X strict_glob # Parse the sublanguage more strictly
229
230<h3 id="ysh:upgrade">ysh:upgrade</h3>
231
232Options in this group enable new YSH features. It doesn't break existing shell
233scripts when it's avoidable.
234
235For example, `parse_at` means that `@myarray` is now the operation to splice
236an array. This will break scripts that expect `@` to be literal, but you can
237simply quote it like `'@literal'` to fix the problem.
238
239 shopt --set ysh:upgrade # turn on all options
240 shopt -p ysh:upgrade # print their current state
241
242Details on each option:
243
244 parse_at echo @array @[arrayfunc(x, y)]
245 parse_brace if true { ... }; cd ~/src { ... }
246 parse_equals x = 'val' in Caps { } config blocks
247 parse_paren if (x > 0) ...
248 parse_proc proc p { ... }
249 parse_triple_quote """$x""" '''x''' (command mode)
250 parse_ysh_string echo r'\' u'\\' b'\\' (command mode)
251 parse_ysh_expr_sub $[] is YSH expression sub, not synonym for $(( ))
252 command_sub_errexit Synchronous errexit check
253 process_sub_fail Analogous to pipefail for process subs
254 sigpipe_status_ok status 141 -> 0 in pipelines
255 simple_word_eval No splitting, static globbing
256 xtrace_rich Hierarchical and process tracing
257 no_xtrace_osh Disable OSH tracing with +
258 no_dash_glob Avoid globbing files like -rf
259 env_obj Init ENV Obj at startup; use it when starting
260 child processes
261 init_ysh_globals Init ARGV List at startup
262 for_loop_frames YSH can create closures from loop vars
263 verbose_errexit Whether to print detailed errors
264 verbose_warn Print various warnings to stderr
265
266<h3 id="ysh:all">ysh:all</h3>
267
268Enable the full YSH language. This includes everything in the `ysh:upgrade`
269group and the `strict:all` group.
270
271 shopt --set ysh:all # turn on all options
272 shopt -p ysh:all # print their current state
273
274Details on options that are not in `ysh:upgrade` and `strict:all`:
275
276 expand_aliases (-u) Whether aliases are expanded
277 parse_at_all @ starting any word is an operator
278 no_parse_backslash Disallow bad backslashes in "" and $''
279 no_parse_backticks Disallow `echo hi`
280 no_parse_bare_word Disallow 'case unquoted' 'for x in unquoted'
281 no_parse_dbracket Disallow legacy booleans [[
282 no_parse_dollar Disallow bare $ for \$ (maybe $/d+/)
283 no_parse_dparen Disallow legacy arithmetic ((
284 no_parse_ignored Don't parse redirects that are ignored
285 no_parse_osh No $'' ( ) & TODO ${x%prefix} ${a[@]} $$
286 no_parse_sh_arith Disallow legacy shell arithmetic
287 no_parse_word_join Disallow pitfall --flag=r'value'
288 no_exported Environ doesn't correspond to exported (-x) vars
289 no_init_globals At startup, don't set vars like PWD, SHELLOPTS
290 no_osh_builtins Disallow OSH builtins like alias, unalias, etc.
291 simple_echo echo doesn't accept -e -n
292 simple_eval_builtin eval takes exactly 1 argument
293 simple_test_builtin test takes 2 or 3 args; use test not [
294 simple_trap_builtin trap doesn't take a code string, etc.
295 X simple_trap Function name only
296
297**Caveat**: Some options only affect shell startup. For example:
298
299- If you start with `osh`, and the script runs `shopt --set ysh:all`, then the
300 YSH [ARGV][] var won't be initialized.
301- In contrast, if you run `osh -o ysh:all -c 'echo @ARGV'`, then [ARGV][] will
302 be initialized. That is, shell startup is done "the YSH way".
303
304[ARGV]: chap-special-var.html#ARGV
305
306## YSH Details
307
308### opts-redefine
309
310In the interactive shell, you can redefine procs and funcs.
311
312 redefine_source 'source-guard' builtin always returns 0
313 X redefine_const Can consts be redefined?
314
315### opts-internal
316
317These options are used by the interpreter. You generally shouldn't set them
318yourself.
319
320 _allow_command_sub To implement strict_errexit, eval_unsafe_arith
321 _allow_process_sub To implement strict_errexit
322 dynamic_scope To implement proc and func
323 _no_debug_trap Used in pipelines in job control shell
324 _running_trap To disable strict_errexit
325 _running_hay Hay evaluation
326
327## Unlinked Descriptions
328
329Here are some descriptions of individual options.
330
331### strict_control_flow
332
333Disallow `break` and `continue` at the top level, and disallow empty args like
334`return $empty`.
335
336### strict_tilde
337
338Failed tilde expansions cause hard errors (like zsh) rather than silently
339evaluating to `~` or `~bad`.
340
341
342### strict_nameref
343
344When `strict_nameref` is set, undefined references produce fatal errors:
345
346 declare -n ref
347 echo $ref # fatal error, not empty string
348 ref=x # fatal error instead of decaying to non-reference
349
350References that don't contain variables also produce hard errors:
351
352 declare -n ref='not a var'
353 echo $ref # fatal
354 ref=x # fatal
355
356### no_parse_ignored
357
358For compatibility, OSH will parse some constructs it doesn't execute, like:
359
360 return 0 2>&1 # redirect on control flow
361
362When this option is disabled, that statement is a syntax error.
363
364### parse_triple_quote
365
366Parse the shell-style multi-line strings, which strip leading whitespace:
367
368 echo '''
369 one
370 two
371 '''
372
373 echo """
374 hello
375 $name
376 """
377
378(This option affects only command mode. Such strings are always parsed in
379expression mode.)
380
381### parse_ysh_string
382
383Allow `r'\'` and `u'\\'` and `b'\\'` strings, as well as their multi-line
384versions.
385
386Since shell strings are already raw, this means that YSH just ignores the r
387prefix:
388
389 echo r'\' # a single backslash
390
391J8 unicode strings:
392
393 echo u'mu \u{3bc}' # mu char
394
395J8 byte strings:
396
397 echo b'byte \yff'
398
399(This option affects only command mode. Such strings are always parsed in
400expression mode.)
401
402### sigpipe_status_ok
403
404If a process that's part of a pipeline exits with status 141 when this is
405option is on, it's turned into status 0, which avoids failure.
406
407SIGPIPE errors occur in cases like 'yes | head', and generally aren't useful.
408