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

423 lines, 274 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 option 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 a 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### ysh_rewrite_extern
200
201This option transparently avoids compatibility workarounds when rewriting
202external commands to **builtins**, providing a performance improvement.
203It might affect the behavior of programs on a POSIX system.
204
205Currently, this only affects [cat][], which otherwise runs in a separate
206process (so that its errors do not crash the main process).
207
208[cat]: chap-builtin-cmd.html#cat
209
210## Groups
211
212To turn OSH into YSH, we use three option groups. Some of them allow new
213features, and some disallow old features.
214
215<!-- note: explicit anchor necessary because of mangling -->
216<h3 id="strict:all">strict:all</h3>
217
218Option in this group disallow problematic or confusing shell constructs. The
219resulting script will still run in another shell.
220
221 shopt --set strict:all # turn on all options
222 shopt -p strict:all # print their current state
223
224Parsing options:
225
226 strict_parse_equals # Disallow '=x' to avoid confusion with '= x'
227 strict_parse_slice # No implicit length for ${a[@]::}
228 X strict_parse_utf8 # Source code must be valid UTF-8
229
230Runtime options:
231
232 strict_arg_parse # Disallow additional arguments to some builtins
233 strict_argv # No empty argv
234 strict_arith # Fatal parse errors (on by default)
235 strict_array # Arrays and strings aren't confused
236 strict_control_flow # Disallow misplaced keyword, empty arg
237 strict_env_binding # Prefix bindings must always be env bindings
238 strict_errexit # Disallow code that ignores failure
239 strict_nameref # Trap invalid variable names
240 strict_word_eval # Expose unicode and slicing errors
241 strict_tilde # Tilde subst can result in error
242 X strict_glob # Parse the sublanguage more strictly
243
244<h3 id="ysh:upgrade">ysh:upgrade</h3>
245
246Options in this group enable new YSH features. It doesn't break existing shell
247scripts when it's avoidable.
248
249For example, `parse_at` means that `@myarray` is now the operation to splice
250an array. This will break scripts that expect `@` to be literal, but you can
251simply quote it like `'@literal'` to fix the problem.
252
253 shopt --set ysh:upgrade # turn on all options
254 shopt -p ysh:upgrade # print their current state
255
256Details on each option:
257
258 parse_at echo @array @[arrayfunc(x, y)]
259 parse_brace if true { ... }; cd ~/src { ... }
260 parse_equals x = 'val' in Caps { } config blocks
261 parse_paren if (x > 0) ...
262 parse_proc proc p { ... }
263 parse_triple_quote """$x""" '''x''' (command mode)
264 parse_ysh_string echo r'\' u'\\' b'\\' (command mode)
265 parse_ysh_expr_sub $[] is YSH expression sub, not synonym for $(( ))
266 command_sub_errexit Synchronous errexit check
267 process_sub_fail Analogous to pipefail for process subs
268 sigpipe_status_ok status 141 -> 0 in pipelines
269 simple_word_eval No splitting, static globbing
270 xtrace_rich Hierarchical and process tracing
271 no_xtrace_osh Disable OSH tracing with +
272 no_dash_glob Avoid globbing files like -rf
273 env_obj Init ENV Obj at startup; use it when starting
274 child processes
275 init_ysh_globals Init ARGV List at startup
276 for_loop_frames YSH can create closures from loop vars
277 verbose_errexit Whether to print detailed errors
278 verbose_warn Print various warnings to stderr
279 ysh_rewrite_extern Avoids compatibility workarounds for builtins
280
281<h3 id="ysh:all">ysh:all</h3>
282
283Enable the full YSH language. This includes everything in the `ysh:upgrade`
284group and the `strict:all` group.
285
286 shopt --set ysh:all # turn on all options
287 shopt -p ysh:all # print their current state
288
289Details on options that are not in `ysh:upgrade` and `strict:all`:
290
291 expand_aliases (-u) Whether aliases are expanded
292 parse_at_all @ starting any word is an operator
293 no_parse_backslash Disallow bad backslashes in "" and $''
294 no_parse_backticks Disallow `echo hi`
295 no_parse_bare_word Disallow 'case unquoted' 'for x in unquoted'
296 no_parse_dbracket Disallow legacy booleans [[
297 no_parse_dollar Disallow bare $ for \$ (maybe $/d+/)
298 no_parse_dparen Disallow legacy arithmetic ((
299 no_parse_ignored Don't parse redirects that are ignored
300 no_parse_osh No $'' ( ) & TODO ${x%prefix} ${a[@]} $$
301 no_parse_sh_arith Disallow legacy shell arithmetic
302 no_parse_word_join Disallow pitfall --flag=r'value'
303 no_exported Environ doesn't correspond to exported (-x) vars
304 no_init_globals At startup, don't set vars like PWD, SHELLOPTS
305 no_osh_builtins Disallow OSH builtins like alias, unalias, etc.
306 simple_echo echo doesn't accept -e -n
307 simple_eval_builtin eval takes exactly 1 argument
308 simple_test_builtin test takes 2 or 3 args; use test not [
309 simple_trap_builtin trap doesn't take a code string, etc.
310 X simple_trap Function name only
311
312**Caveat**: Some options only affect shell startup. For example:
313
314- If you start with `osh`, and the script runs `shopt --set ysh:all`, then the
315 YSH [ARGV][] var won't be initialized.
316- In contrast, if you run `osh -o ysh:all -c 'echo @ARGV'`, then [ARGV][] will
317 be initialized. That is, shell startup is done "the YSH way".
318
319[ARGV]: chap-special-var.html#ARGV
320
321## YSH Details
322
323### opts-redefine
324
325In the interactive shell, you can redefine procs and funcs.
326
327 redefine_source 'source-guard' builtin always returns 0
328 X redefine_const Can consts be redefined?
329
330### opts-internal
331
332These options are used by the interpreter. You generally shouldn't set them
333yourself.
334
335 _allow_command_sub To implement strict_errexit, eval_unsafe_arith
336 _allow_process_sub To implement strict_errexit
337 dynamic_scope To implement proc and func
338 _no_debug_trap Used in pipelines in job control shell
339 _running_trap To disable strict_errexit
340 _running_hay Hay evaluation
341
342## Unlinked Descriptions
343
344Here are some descriptions of individual options.
345
346### strict_control_flow
347
348Disallow `break` and `continue` at the top level, and disallow empty args like
349`return $empty`.
350
351### strict_tilde
352
353Failed tilde expansions cause hard errors (like zsh) rather than silently
354evaluating to `~` or `~bad`.
355
356
357### strict_nameref
358
359When `strict_nameref` is set, undefined references produce fatal errors:
360
361 declare -n ref
362 echo $ref # fatal error, not empty string
363 ref=x # fatal error instead of decaying to non-reference
364
365References that don't contain variables also produce hard errors:
366
367 declare -n ref='not a var'
368 echo $ref # fatal
369 ref=x # fatal
370
371### no_parse_ignored
372
373For compatibility, OSH will parse some constructs it doesn't execute, like:
374
375 return 0 2>&1 # redirect on control flow
376
377When this option is disabled, that statement is a syntax error.
378
379### parse_triple_quote
380
381Parse the shell-style multi-line strings, which strip leading whitespace:
382
383 echo '''
384 one
385 two
386 '''
387
388 echo """
389 hello
390 $name
391 """
392
393(This option affects only command mode. Such strings are always parsed in
394expression mode.)
395
396### parse_ysh_string
397
398Allow `r'\'` and `u'\\'` and `b'\\'` strings, as well as their multi-line
399versions.
400
401Since shell strings are already raw, this means that YSH just ignores the r
402prefix:
403
404 echo r'\' # a single backslash
405
406J8 unicode strings:
407
408 echo u'mu \u{3bc}' # mu char
409
410J8 byte strings:
411
412 echo b'byte \yff'
413
414(This option affects only command mode. Such strings are always parsed in
415expression mode.)
416
417### sigpipe_status_ok
418
419If a process that's part of a pipeline exits with status 141 when this is
420option is on, it's turned into status 0, which avoids failure.
421
422SIGPIPE errors occur in cases like 'yes | head', and generally aren't useful.
423