OILS / doc / ref / chap-front-end.md View on Github | oils.pub

435 lines, 299 significant
1---
2title: Front End (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 **Front End**
13
14</div>
15
16This 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```
32bin/oils-for-unix is an executable that contains OSH, YSH, and more.
33
34Usage:
35 oils-for-unix MAIN_NAME ARG*
36 MAIN_NAME ARG*
37
38It behaves like busybox. The command name can be passed as the first argument:
39
40 oils-for-unix ysh -c 'echo hi'
41
42More commonly, it's invoked through a symlink like 'ysh', which causes it to
43behave 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```
56bin/osh is compatible with POSIX shell, bash, and other shells.
57
58Usage:
59 osh FLAG* SCRIPT ARG*
60 osh FLAG* -c COMMAND ARG*
61 osh FLAG*
62
63Examples:
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```
77bin/ysh is the shell with data tYpes, influenced by pYthon, JavaScript, ...
78
79Usage:
80 ysh FLAG* SCRIPT ARG*
81 ysh FLAG* -c COMMAND ARG*
82 ysh FLAG*
83
84Examples:
85 ysh -c 'echo hi'
86 ysh myscript.ysh
87 echo 'echo hi' | ysh
88
89Note 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```
98osh 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
104They 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
127Examples:
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
136If the --rcfile flag is specified, that file will be executed on startup.
137Otherwise:
138
139- `bin/osh` runs `~/.config/oils/oshrc`
140- `bin/ysh` runs `~/.config/oils/yshrc`
141
142Pass --rcfile /dev/null or --norc to disable the startup file.
143
144If the --rcdir flag is specified, files in that folder will be executed on
145startup.
146Otherwise:
147
148- `bin/osh` runs everything in `~/.config/oils/oshrc.d/`
149- `bin/ysh` runs everything in `~/.config/oils/yshrc.d/`
150
151Pass --norc to disable the startup directory.
152
153<h3 id="startup" class="osh-ysh-topic">startup</h3>
154
155TODO:
156
1571. History is read
1581. ...
159
160<h3 id="line-editing" class="osh-ysh-topic">line-editing</h3>
161
162Oils is often built with GNU readline, which recognizes many terminal commands
163for editing input.
164
165A useful option is `set -o vi`, which tells GNU readline to accept vi keys.
166
167### exit-codes
168
169The meaning of exit codes is a convention, and generally follows one of two
170paradigms.
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- YSH uses exit codes 3 and greater for certain failures. See
183 [ysh-exit-codes](#ysh-exit-codes).
184
185
186POSIX exit codes:
187
188- `126` for permission denied when running a command (`errno EACCES`)
189- `127` for command not found
190
191Hint: Error checking often looks like this:
192
193 try {
194 ls /bad
195 }
196 if (_error.code !== 0) {
197 echo 'failed'
198 }
199
200#### The Boolean Paradigm
201
202- `0` for **true**
203- `1` for **false**.
204 - Example: `test -f foo` and `foo` isn't a file.
205- `2` for **error** (usage error, parse error, etc.)
206 - Example: `test -q`: the flag isn't accepted.
207
208Hint: The `boolstatus` builtin ensures that false and error aren't confused:
209
210 if boolstatus test -f foo {
211 echo 'foo exists'
212 }
213
214See [YSH Fixes Shell's Error Handling](../error-handling.html) for more detail.
215
216### ysh-exit-codes
217
218- `3` for expression errors
219 - e.g. for [List][] index out of bounds, or divide by zero
220- `4` for encoding and decoding errors
221 - e.g. used by the [json][] builtin.
222- `10` is the default error code for the [error][] builtin
223
224Related: the [`_error`][error-dict] dict.
225
226[List]: chap-type-method.html#List
227[json]: chap-builtin-cmd.html#json
228[error]: chap-builtin-cmd.html#error
229[error-dict]: chap-special-var.html#_error
230
231## Lexing
232
233<h3 id="comment" class="osh-ysh-topic">comment</h3>
234
235A comment starts with `#` and goes until the end of the line.
236
237 echo hi # print a greeting
238
239<h3 id="line-continuation" class="osh-ysh-topic">line-continuation</h3>
240
241A backslash `\` at the end of a line continues the line without executing it:
242
243 ls /usr/bin \
244 /usr/lib \
245 ~/src # A single command split over three lines
246
247<h3 id="ascii-whitespace" class="osh-ysh-topic">ascii-whitespace</h3>
248
249In most places, Oils uses the same definition of ASCII whitespace as JSON.
250That is, any of these 4 bytes are considered whitespace:
251
252 [ \t\r\n] # space, tab, carriage return, newline
253
254Sometimes newlines are significant, e.g. after shell commands. Then the set of
255whitespace characters is:
256
257 [ \t\r]
258
259(We don't handle the Windows `\r\n` sequence in a special way. Instead, `\r`
260is often treated like space and tab.)
261
262Examples:
263
264- Inside shell arithmetic `$(( 1 + 2 ))`, ASCII whitespace is ignored.
265- Inside YSH expressions `42 + a[i] * f(x)`, ASCII whitespace is ignored.
266
267Exceptions:
268
269- Carriage return `\r` may not always be whitespace.
270 - It can appear in an unquoted shell words, a rule that all POSIX shells
271 follow.
272 - The default `$IFS` doesn't include `\r`.
273- YSH `trim()` functions also respect Unicode space.
274
275<h3 id="ascii-control-chars" class="osh-ysh-topic">ascii-control-chars</h3>
276
277The ASCII control chars have byte values `0x00` to `0x1F`. This set includes 3
278whitespace chars:
279
280- tab - `\t` aka `0x09`
281- newline - `\n` aka `0x0a`
282- carriage return - `\r` aka `0x0d`
283
284(It doesn't include the space - `0x20`.)
285
286General rules:
287
288- In J8 **data** languages, control chars other than whitespace are illegal.
289 This is consistent with the JSON spec.
290- In **source code**, control chars are allowed (but discouraged).
291 - For example, in OSH, we don't check for control chars unquoted words
292 words or string literals. They are treated like printable chars.
293 - TODO: YSH should only allow printable characters, which implies valid
294 UTF-8.
295
296Note about `NUL` aka `0x00`:
297
298- The NUL byte is often used to terminate buffers, i.e. as a sentinel for
299 [re2c](https://re2c.org) lexing. This means that data after the NUL will be
300 ignored.
301 - J8 **data** input is read all at once, i.e. **not** split into lines. So
302 everything after the first NUL may be ignored.
303 - Shell **source code** is split into lines.
304
305<h3 id="doc-comment" class="ysh-topic">doc-comment</h3>
306
307Doc comments can be attached to procs or shell functions:
308
309 proc deploy {
310 ### Deploy the app
311 echo hi
312 }
313
314 my_sh_function() {
315 ### This also works
316 echo hi
317 }
318
319<h3 id="multiline-command" class="ysh-topic">multiline-command</h3>
320
321The ... prefix starts a single command over multiple lines. It allows writing
322long commands without \ continuation lines, and the resulting limitations on
323where you can put comments.
324
325Single command example:
326
327 ... chromium-browser
328 # comment on its own line
329 --no-proxy-server
330 --incognito # comment to the right
331 ;
332
333Long pipelines and and-or chains:
334
335 ... find .
336 # exclude tests
337 | grep -v '_test.py'
338 | xargs wc -l
339 | sort -n
340 ;
341
342 ... ls /
343 && ls /bin
344 && ls /lib
345 || error "oops"
346 ;
347
348## Tools
349
350### cat-em
351
352Print files embedded in the `oils-for-unix` binary to stdout. Example:
353
354 osh --tool cat-em stdlib/math.ysh stdlib/other.ysh
355
356### syntax-tree
357
358Print the syntax tree in a debug format.
359
360 osh --tool syntax-tree stdlib/ysh/math.ysh
361
362The `-n` flag is a shortcut:
363
364 osh -n stdlib/ysh/math.ysh
365
366
367## Help Chapters
368
369<h3 id="osh-chapters" class="osh-topic" oils-embed="1">
370 osh-chapters
371</h3>
372
373<!-- shown at the bottom of 'help' -->
374
375```
376The reference is divided in to "chapters", each of which has its own table of
377contents. Type:
378
379 help osh-$CHAPTER
380
381Where $CHAPTER is one of:
382
383 type-method
384 builtin-cmd
385 stdlib
386 front-end
387 cmd-lang
388 osh-assign
389 word-lang
390 mini-lang
391 option
392 special-var
393 plugin
394
395Example:
396
397 help osh-word-lang
398```
399
400
401<h3 id="ysh-chapters" class="ysh-topic" oils-embed="1">
402 ysh-chapters
403</h3>
404
405<!-- shown at the bottom of 'help' -->
406
407```
408The reference is divided in to "chapters", each of which has its own table of
409contents. Type:
410
411 help ysh-$CHAPTER
412
413Where $CHAPTER is one of:
414
415 type-method
416 builtin-func
417 builtin-cmd
418 stdlib
419 front-end
420 cmd-lang
421 ysh-cmd
422 expr-lang
423 word-lang
424 option
425 special-var
426 plugin
427
428Example:
429
430 help ysh-expr-lang
431 help ysh-ysh-cmd # may change
432```
433
434<!-- h4 needed to end last card: ysh-chapters -->
435<h4></h4>