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