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

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