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

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