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

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