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