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

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