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

292 lines, 199 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="doc-comment" class="ysh-topic">doc-comment</h3>
184
185Doc comments look like this:
186
187 proc deploy {
188 ### Deploy the app
189 echo hi
190 }
191
192<h3 id="multiline-command" class="ysh-topic">multiline-command</h3>
193
194The ... prefix starts a single command over multiple lines. It allows writing
195long commands without \ continuation lines, and the resulting limitations on
196where you can put comments.
197
198Single command example:
199
200 ... chromium-browser
201 # comment on its own line
202 --no-proxy-server
203 --incognito # comment to the right
204 ;
205
206Long pipelines and and-or chains:
207
208 ... find .
209 # exclude tests
210 | grep -v '_test.py'
211 | xargs wc -l
212 | sort -n
213 ;
214
215 ... ls /
216 && ls /bin
217 && ls /lib
218 || error "oops"
219 ;
220
221## Tools
222
223### cat-em
224
225Print files embedded in the `oils-for-unix` binary to stdout. Example:
226
227 osh --tool cat-em stdlib/math.ysh stdlib/other.ysh
228
229
230## Help Chapters
231
232<h3 id="osh-chapters" class="osh-topic" oils-embed="1">
233 osh-chapters
234</h3>
235
236<!-- shown at the bottom of 'help' -->
237
238```
239The reference is divided in to "chapters", each of which has its own table of
240contents. Type:
241
242 help osh-$CHAPTER
243
244Where $CHAPTER is one of:
245
246 front-end
247 command-lang
248 osh-assign
249 word-lang
250 mini-lang
251 builtin-cmd
252 option
253 special-var
254 plugin
255
256Example:
257
258 help osh-word-lang
259```
260
261
262<h3 id="ysh-chapters" class="ysh-topic" oils-embed="1">
263 ysh-chapters
264</h3>
265
266<!-- shown at the bottom of 'help' -->
267
268```
269The reference is divided in to "chapters", each of which has its own table of
270contents. Type:
271
272 help ysh-$CHAPTER
273
274Where $CHAPTER is one of:
275
276 front-end
277 command-lang
278 expr-lang
279 word-lang
280 builtin-cmd
281 option
282 special-var
283 type-method
284 builtin-func
285
286Example:
287
288 help ysh-expr-lang
289```
290
291<!-- h4 needed to end last card: ysh-chapters -->
292<h4></h4>