OILS / doc / ref / chap-option.md View on Github | oilshell.org

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