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