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

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