| 1 | ---
|
| 2 | title: Front End (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 **Front End**
|
| 13 |
|
| 14 | </div>
|
| 15 |
|
| 16 | This chapter describes command line usage and lexing.
|
| 17 |
|
| 18 | <span class="in-progress">(in progress)</span>
|
| 19 |
|
| 20 | <div id="dense-toc">
|
| 21 | </div>
|
| 22 |
|
| 23 | <h2 id="usage">Command Line Usage</h2>
|
| 24 |
|
| 25 | <h3 id="oils-usage" class="osh-ysh-topic" oils-embed="1">
|
| 26 | oils-usage
|
| 27 | </h3>
|
| 28 |
|
| 29 | <!-- pre-formatted for help builtin -->
|
| 30 |
|
| 31 | ```
|
| 32 | bin/oils-for-unix is an executable that contains OSH, YSH, and more.
|
| 33 |
|
| 34 | Usage:
|
| 35 | oils-for-unix MAIN_NAME ARG*
|
| 36 | MAIN_NAME ARG*
|
| 37 |
|
| 38 | It behaves like busybox. The command name can be passed as the first argument:
|
| 39 |
|
| 40 | oils-for-unix ysh -c 'echo hi'
|
| 41 |
|
| 42 | More commonly, it's invoked through a symlink like 'ysh', which causes it to
|
| 43 | behave like that command:
|
| 44 |
|
| 45 | ysh -c 'echo hi'
|
| 46 |
|
| 47 | ```
|
| 48 |
|
| 49 | <h3 id="osh-usage" class="osh-topic" oils-embed="1">
|
| 50 | osh-usage
|
| 51 | </h3>
|
| 52 |
|
| 53 | <!-- pre-formatted for help builtin -->
|
| 54 |
|
| 55 | ```
|
| 56 | bin/osh is compatible with POSIX shell, bash, and other shells.
|
| 57 |
|
| 58 | Usage:
|
| 59 | osh FLAG* SCRIPT ARG*
|
| 60 | osh FLAG* -c COMMAND ARG*
|
| 61 | osh FLAG*
|
| 62 |
|
| 63 | Examples:
|
| 64 | osh -c 'echo hi'
|
| 65 | osh myscript.sh
|
| 66 | echo 'echo hi' | osh
|
| 67 |
|
| 68 | ```
|
| 69 |
|
| 70 | <h3 id="ysh-usage" class="ysh-topic" oils-embed="1">
|
| 71 | ysh-usage
|
| 72 | </h3>
|
| 73 |
|
| 74 | <!-- pre-formatted for help builtin -->
|
| 75 |
|
| 76 | ```
|
| 77 | bin/ysh is the shell with data tYpes, influenced by pYthon, JavaScript, ...
|
| 78 |
|
| 79 | Usage:
|
| 80 | ysh FLAG* SCRIPT ARG*
|
| 81 | ysh FLAG* -c COMMAND ARG*
|
| 82 | ysh FLAG*
|
| 83 |
|
| 84 | Examples:
|
| 85 | ysh -c 'echo hi'
|
| 86 | ysh myscript.ysh
|
| 87 | echo 'echo hi' | ysh
|
| 88 |
|
| 89 | Note that bin/ysh is the same as bin/osh with the ysh:all option group set:
|
| 90 | osh -o ysh:all -c 'echo hi' # Same as YSH
|
| 91 | ```
|
| 92 |
|
| 93 | <h3 id="shell-flags" oils-embed="1">
|
| 94 | shell-flags
|
| 95 | </h3>
|
| 96 |
|
| 97 | ```
|
| 98 | osh and ysh accept standard POSIX shell flags, like:
|
| 99 |
|
| 100 | bin/osh -o errexit -c 'false'
|
| 101 | bin/ysh -n myfile.ysh
|
| 102 | bin/ysh +o errexit -c 'false; echo ok'
|
| 103 |
|
| 104 | They also accept these flags:
|
| 105 |
|
| 106 | --eval FILE
|
| 107 | Evaluate the given file, similar to the 'source' builtin. Specify it
|
| 108 | multiple times to run multiple files.
|
| 109 |
|
| 110 | If the errexit option is on (e.g. in YSH), then the shell stops when $?
|
| 111 | is non-zero after evaluating a file. The $0 value is set to FILE.
|
| 112 |
|
| 113 | --eval-pure FILE
|
| 114 | Like --eval, but disallow I/O (known as "pure mode").
|
| 115 |
|
| 116 | --location-str
|
| 117 | Use this string to display error messages.
|
| 118 | See 'help sourceCode' for an example.
|
| 119 |
|
| 120 | --location-start-line
|
| 121 | Use this line number offset to display error messages.
|
| 122 |
|
| 123 | --tool Run a tool instead of the shell (cat-em|syntax-tree)
|
| 124 | -n Parse the program but don't execute it. Print the AST.
|
| 125 | --ast-format FMT The format for the AST (text|text-abbrev)
|
| 126 |
|
| 127 | Examples:
|
| 128 |
|
| 129 | ysh --eval one.ysh --eval two.ysh -c 'echo hi' # Run 2 files first
|
| 130 | osh -n -c 'hello' # pretty-print the AST
|
| 131 | ysh --ast-format text -n -c 'hello' # in unabridged format
|
| 132 | ```
|
| 133 |
|
| 134 | <h3 id="config" class="osh-ysh-topic">config</h3>
|
| 135 |
|
| 136 | If the --rcfile flag is specified, that file will be executed on startup.
|
| 137 | Otherwise:
|
| 138 |
|
| 139 | - `bin/osh` runs `~/.config/oils/oshrc`
|
| 140 | - `bin/ysh` runs `~/.config/oils/yshrc`
|
| 141 |
|
| 142 | Pass --rcfile /dev/null or --norc to disable the startup file.
|
| 143 |
|
| 144 | If the --rcdir flag is specified, files in that folder will be executed on
|
| 145 | startup.
|
| 146 | Otherwise:
|
| 147 |
|
| 148 | - `bin/osh` runs everything in `~/.config/oils/oshrc.d/`
|
| 149 | - `bin/ysh` runs everything in `~/.config/oils/yshrc.d/`
|
| 150 |
|
| 151 | Pass --norc to disable the startup directory.
|
| 152 |
|
| 153 | <h3 id="startup" class="osh-ysh-topic">startup</h3>
|
| 154 |
|
| 155 | TODO:
|
| 156 |
|
| 157 | 1. History is read
|
| 158 | 1. ...
|
| 159 |
|
| 160 | <h3 id="line-editing" class="osh-ysh-topic">line-editing</h3>
|
| 161 |
|
| 162 | Oils is often built with GNU readline, which recognizes many terminal commands
|
| 163 | for editing input.
|
| 164 |
|
| 165 | A useful option is `set -o vi`, which tells GNU readline to accept vi keys.
|
| 166 |
|
| 167 | <h3 id="exit-codes" class="osh-ysh-topic">exit-codes</h3>
|
| 168 |
|
| 169 | The meaning of exit codes is a convention, and generally follows one of two
|
| 170 | paradigms.
|
| 171 |
|
| 172 | #### The Success / Failure Paradigm
|
| 173 |
|
| 174 | - `0` for **success**.
|
| 175 | - `1` for **runtime error**
|
| 176 | - Example: `echo foo > out.txt` and `out.txt` can't be opened.
|
| 177 | - Example: `fg` and there's not job to put in the foreground.
|
| 178 | - `2` for **parse error**. This means that we didn't *attempt* to do
|
| 179 | anything, rather than doing something, then it fails.
|
| 180 | - Example: A language parse error, like `echo $(`.
|
| 181 | - Example: Builtin usage error, like `read -z`.
|
| 182 | - `3` for runtime **expression errors**. The expression language is new to
|
| 183 | Oils, so its errors have a new exit code.
|
| 184 | - Example: divide by zero `42 / 0`
|
| 185 | - Example: index out of range `a[1000]`
|
| 186 |
|
| 187 | POSIX exit codes:
|
| 188 |
|
| 189 | - `126` for permission denied when running a command (`errno EACCES`)
|
| 190 | - `127` for command not found
|
| 191 |
|
| 192 | Hint: Error checking often looks like this:
|
| 193 |
|
| 194 | try {
|
| 195 | ls /bad
|
| 196 | }
|
| 197 | if (_error.code !== 0) {
|
| 198 | echo 'failed'
|
| 199 | }
|
| 200 |
|
| 201 | #### The Boolean Paradigm
|
| 202 |
|
| 203 | - `0` for **true**
|
| 204 | - `1` for **false**.
|
| 205 | - Example: `test -f foo` and `foo` isn't a file.
|
| 206 | - `2` for **error** (usage error, parse error, etc.)
|
| 207 | - Example: `test -q`: the flag isn't accepted.
|
| 208 |
|
| 209 | Hint: The `boolstatus` builtin ensures that false and error aren't confused:
|
| 210 |
|
| 211 | if boolstatus test -f foo {
|
| 212 | echo 'foo exists'
|
| 213 | }
|
| 214 |
|
| 215 | See [YSH Fixes Shell's Error Handling](../error-handling.html) for more detail.
|
| 216 |
|
| 217 | ## Lexing
|
| 218 |
|
| 219 | <h3 id="comment" class="osh-ysh-topic">comment</h3>
|
| 220 |
|
| 221 | A comment starts with `#` and goes until the end of the line.
|
| 222 |
|
| 223 | echo hi # print a greeting
|
| 224 |
|
| 225 | <h3 id="line-continuation" class="osh-ysh-topic">line-continuation</h3>
|
| 226 |
|
| 227 | A backslash `\` at the end of a line continues the line without executing it:
|
| 228 |
|
| 229 | ls /usr/bin \
|
| 230 | /usr/lib \
|
| 231 | ~/src # A single command split over three lines
|
| 232 |
|
| 233 | <h3 id="ascii-whitespace" class="osh-ysh-topic">ascii-whitespace</h3>
|
| 234 |
|
| 235 | In most places, Oils uses the same definition of ASCII whitespace as JSON.
|
| 236 | That is, any of these 4 bytes are considered whitespace:
|
| 237 |
|
| 238 | [ \t\r\n] # space, tab, carriage return, newline
|
| 239 |
|
| 240 | Sometimes newlines are significant, e.g. after shell commands. Then the set of
|
| 241 | whitespace characters is:
|
| 242 |
|
| 243 | [ \t\r]
|
| 244 |
|
| 245 | (We don't handle the Windows `\r\n` sequence in a special way. Instead, `\r`
|
| 246 | is often treated like space and tab.)
|
| 247 |
|
| 248 | Examples:
|
| 249 |
|
| 250 | - Inside shell arithmetic `$(( 1 + 2 ))`, ASCII whitespace is ignored.
|
| 251 | - Inside YSH expressions `42 + a[i] * f(x)`, ASCII whitespace is ignored.
|
| 252 |
|
| 253 | Exceptions:
|
| 254 |
|
| 255 | - Carriage return `\r` may not always be whitespace.
|
| 256 | - It can appear in an unquoted shell words, a rule that all POSIX shells
|
| 257 | follow.
|
| 258 | - The default `$IFS` doesn't include `\r`.
|
| 259 | - YSH `trim()` functions also respect Unicode space.
|
| 260 |
|
| 261 | <h3 id="ascii-control-chars" class="osh-ysh-topic">ascii-control-chars</h3>
|
| 262 |
|
| 263 | The ASCII control chars have byte values `0x00` to `0x1F`. This set includes 3
|
| 264 | whitespace chars:
|
| 265 |
|
| 266 | - tab - `\t` aka `0x09`
|
| 267 | - newline - `\n` aka `0x0a`
|
| 268 | - carriage return - `\r` aka `0x0d`
|
| 269 |
|
| 270 | (It doesn't include the space - `0x20`.)
|
| 271 |
|
| 272 | General rules:
|
| 273 |
|
| 274 | - In J8 **data** languages, control chars other than whitespace are illegal.
|
| 275 | This is consistent with the JSON spec.
|
| 276 | - In **source code**, control chars are allowed (but discouraged).
|
| 277 | - For example, in OSH, we don't check for control chars unquoted words
|
| 278 | words or string literals. They are treated like printable chars.
|
| 279 | - TODO: YSH should only allow printable characters, which implies valid
|
| 280 | UTF-8.
|
| 281 |
|
| 282 | Note about `NUL` aka `0x00`:
|
| 283 |
|
| 284 | - The NUL byte is often used to terminate buffers, i.e. as a sentinel for
|
| 285 | [re2c](https://re2c.org) lexing. This means that data after the NUL will be
|
| 286 | ignored.
|
| 287 | - J8 **data** input is read all at once, i.e. **not** split into lines. So
|
| 288 | everything after the first NUL may be ignored.
|
| 289 | - Shell **source code** is split into lines.
|
| 290 |
|
| 291 | <h3 id="doc-comment" class="ysh-topic">doc-comment</h3>
|
| 292 |
|
| 293 | Doc comments can be attached to procs or shell functions:
|
| 294 |
|
| 295 | proc deploy {
|
| 296 | ### Deploy the app
|
| 297 | echo hi
|
| 298 | }
|
| 299 |
|
| 300 | my_sh_function() {
|
| 301 | ### This also works
|
| 302 | echo hi
|
| 303 | }
|
| 304 |
|
| 305 | <h3 id="multiline-command" class="ysh-topic">multiline-command</h3>
|
| 306 |
|
| 307 | The ... prefix starts a single command over multiple lines. It allows writing
|
| 308 | long commands without \ continuation lines, and the resulting limitations on
|
| 309 | where you can put comments.
|
| 310 |
|
| 311 | Single command example:
|
| 312 |
|
| 313 | ... chromium-browser
|
| 314 | # comment on its own line
|
| 315 | --no-proxy-server
|
| 316 | --incognito # comment to the right
|
| 317 | ;
|
| 318 |
|
| 319 | Long pipelines and and-or chains:
|
| 320 |
|
| 321 | ... find .
|
| 322 | # exclude tests
|
| 323 | | grep -v '_test.py'
|
| 324 | | xargs wc -l
|
| 325 | | sort -n
|
| 326 | ;
|
| 327 |
|
| 328 | ... ls /
|
| 329 | && ls /bin
|
| 330 | && ls /lib
|
| 331 | || error "oops"
|
| 332 | ;
|
| 333 |
|
| 334 | ## Tools
|
| 335 |
|
| 336 | ### cat-em
|
| 337 |
|
| 338 | Print files embedded in the `oils-for-unix` binary to stdout. Example:
|
| 339 |
|
| 340 | osh --tool cat-em stdlib/math.ysh stdlib/other.ysh
|
| 341 |
|
| 342 | ### syntax-tree
|
| 343 |
|
| 344 | Print the syntax tree in a debug format.
|
| 345 |
|
| 346 | osh --tool syntax-tree stdlib/ysh/math.ysh
|
| 347 |
|
| 348 | The `-n` flag is a shortcut:
|
| 349 |
|
| 350 | osh -n stdlib/ysh/math.ysh
|
| 351 |
|
| 352 |
|
| 353 | ## Help Chapters
|
| 354 |
|
| 355 | <h3 id="osh-chapters" class="osh-topic" oils-embed="1">
|
| 356 | osh-chapters
|
| 357 | </h3>
|
| 358 |
|
| 359 | <!-- shown at the bottom of 'help' -->
|
| 360 |
|
| 361 | ```
|
| 362 | The reference is divided in to "chapters", each of which has its own table of
|
| 363 | contents. Type:
|
| 364 |
|
| 365 | help osh-$CHAPTER
|
| 366 |
|
| 367 | Where $CHAPTER is one of:
|
| 368 |
|
| 369 | type-method
|
| 370 | builtin-cmd
|
| 371 | stdlib
|
| 372 | front-end
|
| 373 | cmd-lang
|
| 374 | osh-assign
|
| 375 | word-lang
|
| 376 | mini-lang
|
| 377 | option
|
| 378 | special-var
|
| 379 | plugin
|
| 380 |
|
| 381 | Example:
|
| 382 |
|
| 383 | help osh-word-lang
|
| 384 | ```
|
| 385 |
|
| 386 |
|
| 387 | <h3 id="ysh-chapters" class="ysh-topic" oils-embed="1">
|
| 388 | ysh-chapters
|
| 389 | </h3>
|
| 390 |
|
| 391 | <!-- shown at the bottom of 'help' -->
|
| 392 |
|
| 393 | ```
|
| 394 | The reference is divided in to "chapters", each of which has its own table of
|
| 395 | contents. Type:
|
| 396 |
|
| 397 | help ysh-$CHAPTER
|
| 398 |
|
| 399 | Where $CHAPTER is one of:
|
| 400 |
|
| 401 | type-method
|
| 402 | builtin-func
|
| 403 | builtin-cmd
|
| 404 | stdlib
|
| 405 | front-end
|
| 406 | cmd-lang
|
| 407 | ysh-cmd
|
| 408 | expr-lang
|
| 409 | word-lang
|
| 410 | option
|
| 411 | special-var
|
| 412 | plugin
|
| 413 |
|
| 414 | Example:
|
| 415 |
|
| 416 | help ysh-expr-lang
|
| 417 | help ysh-ysh-cmd # may change
|
| 418 | ```
|
| 419 |
|
| 420 | <!-- h4 needed to end last card: ysh-chapters -->
|
| 421 | <h4></h4>
|