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

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