1 | ---
|
2 | in_progress: yes
|
3 | body_css_class: width40 help-body
|
4 | default_highlighter: oils-sh
|
5 | preserve_anchor_case: yes
|
6 | ---
|
7 |
|
8 | Command Language
|
9 | ===
|
10 |
|
11 | This chapter in the [Oils Reference](index.html) describes the command language
|
12 | for both OSH and YSH.
|
13 |
|
14 | <div id="toc">
|
15 | </div>
|
16 |
|
17 | ## Quick Sketch: What's a Command?
|
18 |
|
19 | OSH:
|
20 |
|
21 | print-files() {
|
22 | for name in *.py; do
|
23 | if test -x "$name"; then
|
24 | echo "$name is executable"
|
25 | fi
|
26 | done
|
27 | }
|
28 |
|
29 | YSH:
|
30 |
|
31 | proc print-files {
|
32 | for name in *.py {
|
33 | if test -x $name { # no quotes needed
|
34 | echo "$name is executable"
|
35 | }
|
36 | }
|
37 | }
|
38 |
|
39 |
|
40 | <h2 id="Commands">Commands</h2>
|
41 |
|
42 | <h3 id="simple-command" class="osh-ysh-topic">simple-command</h3>
|
43 |
|
44 | Commands are composed of words. The first word may be the name of
|
45 |
|
46 | 1. A builtin shell command
|
47 | 1. A YSH `proc` or shell "function"
|
48 | 1. A Hay node declared with `hay define`
|
49 | 1. An external command
|
50 | 1. An alias
|
51 |
|
52 | Examples:
|
53 |
|
54 | echo hi # a shell builtin doesn't start a process
|
55 | ls /usr/bin ~/src # starts a new process
|
56 | myproc "hello $name"
|
57 | myshellfunc "hello $name"
|
58 | myalias -l
|
59 | <!-- TODO: document lookup order -->
|
60 |
|
61 | Redirects are also allowed in any part of the command:
|
62 |
|
63 | echo 'to stderr' >&2
|
64 | echo >&2 'to stderr'
|
65 |
|
66 | echo 'to file' > out.txt
|
67 | echo > out.txt 'to file'
|
68 |
|
69 | <h3 id="semicolon" class="osh-ysh-topic">semicolon ;</h3>
|
70 |
|
71 | Run two commands in sequence like this:
|
72 |
|
73 | echo one; echo two
|
74 |
|
75 | or this:
|
76 |
|
77 | echo one
|
78 | echo two
|
79 |
|
80 | <h2 id="Conditional">Conditional</h2>
|
81 |
|
82 | <h3 id="case" class="osh-topic">case</h3>
|
83 |
|
84 | Match a string against a series of glob patterns. Execute code in the section
|
85 | below the matching pattern.
|
86 |
|
87 | path='foo.py'
|
88 | case "$path" in
|
89 | *.py)
|
90 | echo 'python'
|
91 | ;;
|
92 | *.sh)
|
93 | echo 'shell'
|
94 | ;;
|
95 | esac
|
96 |
|
97 | For bash compatibility, the `;;` terminator can be substituted with either:
|
98 |
|
99 | - `;&` - fall through to next arm, ignoring the condition
|
100 | - `;;&` - fall through to next arm, respecting the condition
|
101 |
|
102 | <h3 id="if" class="osh-topic">if</h3>
|
103 |
|
104 | Test if a command exited with status zero (true). If so, execute the
|
105 | corresponding block of code.
|
106 |
|
107 | Shell:
|
108 |
|
109 | if test -d foo; then
|
110 | echo 'foo is a directory'
|
111 | elif test -f foo; then
|
112 | echo 'foo is a file'
|
113 | else
|
114 | echo 'neither'
|
115 | fi
|
116 |
|
117 | YSH:
|
118 |
|
119 | if test -d foo {
|
120 | echo 'foo is a directory'
|
121 | } elif test -f foo {
|
122 | echo 'foo is a file'
|
123 | } else {
|
124 | echo 'neither'
|
125 | }
|
126 |
|
127 | <h3 id="true" class="osh-ysh-topic">true</h3>
|
128 |
|
129 | Do nothing and return status 0.
|
130 |
|
131 | if true; then
|
132 | echo hello
|
133 | fi
|
134 |
|
135 | <h3 id="false" class="osh-ysh-topic">false</h3>
|
136 |
|
137 | Do nothing and return status 1.
|
138 |
|
139 | if false; then
|
140 | echo 'not reached'
|
141 | else
|
142 | echo hello
|
143 | fi
|
144 |
|
145 | <h3 id="colon" class="osh-topic">colon :</h3>
|
146 |
|
147 | Like `true`: do nothing and return status 0.
|
148 |
|
149 | <h3 id="bang" class="osh-ysh-topic">bang !</h3>
|
150 |
|
151 | Invert an exit code:
|
152 |
|
153 | if ! test -d /tmp; then
|
154 | echo "No temp directory
|
155 | fi
|
156 |
|
157 | <h3 id="and" class="osh-ysh-topic">and &&</h3>
|
158 |
|
159 | mkdir -p /tmp && cp foo /tmp
|
160 |
|
161 | <h3 id="or" class="osh-ysh-topic">or ||</h3>
|
162 |
|
163 | ls || die "failed"
|
164 |
|
165 | <h2 id="Iteration">Iteration</h2>
|
166 |
|
167 | <h3 id="while" class="osh-ysh-topic">while</h3>
|
168 |
|
169 | POSIX
|
170 |
|
171 | <h3 id="until" class="osh-topic">until</h3>
|
172 |
|
173 | POSIX
|
174 |
|
175 | <h3 id="for" class="osh-ysh-topic">for</h3>
|
176 |
|
177 | For loops iterate over words.
|
178 |
|
179 | YSH style:
|
180 |
|
181 | var mystr = 'one'
|
182 | var myarray = :| two three |
|
183 |
|
184 | for i in $mystr @myarray *.py {
|
185 | echo $i
|
186 | }
|
187 |
|
188 |
|
189 | Shell style:
|
190 |
|
191 | local mystr='one'
|
192 | local myarray=(two three)
|
193 |
|
194 | for i in "mystr" "${myarray[@]}" *.py; do
|
195 | echo $i
|
196 | done
|
197 |
|
198 | Both fragments output 3 lines and then Python files on remaining lines.
|
199 |
|
200 | <h3 id="for-expr-sh" class="osh-topic">for-expr-sh</h3>
|
201 |
|
202 | A bash/ksh construct:
|
203 |
|
204 | for (( i = 0; i < 5; ++i )); do
|
205 | echo $i
|
206 | done
|
207 |
|
208 | <h2 id="Control Flow">Control Flow</h2>
|
209 |
|
210 | These are keywords in Oils, not builtins!
|
211 |
|
212 | ### break
|
213 |
|
214 | Break out of a loop. (Not used for case statements!)
|
215 |
|
216 | ### continue
|
217 |
|
218 | Continue to the next iteration of a loop.
|
219 |
|
220 | ### return
|
221 |
|
222 | Return from a function.
|
223 |
|
224 | ### exit
|
225 |
|
226 | Exit the shell process with the given status:
|
227 |
|
228 | exit 2
|
229 |
|
230 | <h2 id="Grouping">Grouping</h2>
|
231 |
|
232 | ### sh-func
|
233 |
|
234 | POSIX:
|
235 |
|
236 | f() {
|
237 | echo args "$@"
|
238 | }
|
239 | f 1 2 3
|
240 |
|
241 | ### sh-block
|
242 |
|
243 | POSIX:
|
244 |
|
245 | { echo one; echo two; }
|
246 |
|
247 | The trailing `;` is necessary in OSH, but not YSH. In YSH, `parse_brace` makes
|
248 | `}` is more of a special word.
|
249 |
|
250 |
|
251 | ### subshell
|
252 |
|
253 | ( echo one; echo two )
|
254 |
|
255 | Use [forkwait]($osh-help) in YSH instead.
|
256 |
|
257 | <h2 id="Concurrency">Concurrency</h2>
|
258 |
|
259 | ### pipe
|
260 |
|
261 | ### ampersand
|
262 |
|
263 | CMD &
|
264 |
|
265 | The `&` language construct runs CMD in the background as a job, immediately
|
266 | returning control to the shell.
|
267 |
|
268 | The resulting PID is recorded in the `$!` variable.
|
269 |
|
270 | <h2 id="Redirects">Redirects</h2>
|
271 |
|
272 | ### redir-file
|
273 |
|
274 | Examples of redirecting the `stdout` of a command:
|
275 |
|
276 | echo foo > out.txt # overwrite out.txt
|
277 | date >> stamp.txt # append to stamp.txt
|
278 |
|
279 | <!--
|
280 | echo foo >| out.txt # clobber the file even if set -o noclobber
|
281 | -->
|
282 |
|
283 | Redirect to the `stdin` of a command:
|
284 |
|
285 | cat < in.txt
|
286 |
|
287 | Redirects are compatible with POSIX and bash, so they take descriptor numbers
|
288 | on the left:
|
289 |
|
290 | make 2> stderr.txt # '2>' is valid, but '2 >' is not
|
291 |
|
292 | Note that the word argument to **file** redirects is evaluated like bash, which
|
293 | is different than other arguments to other redirects:
|
294 |
|
295 | tar -x -z < Python* # glob must expand to exactly 1 file
|
296 | tar -x -z < $myvar # $myvar is split because it's unquoted
|
297 |
|
298 | In other words, it's evaluated **as** a sequence of 1 word, which **produces**
|
299 | zero to N strings. But redirects are only valid when it produces exactly 1
|
300 | string.
|
301 |
|
302 | (Related: YSH uses `shopt --set simple_word_eval`, which means that globs that
|
303 | match nothing evaluate to zero strings, not themselves.)
|
304 |
|
305 | <!-- They also take a file descriptor on the left -->
|
306 |
|
307 |
|
308 | ### redir-desc
|
309 |
|
310 | Redirect to a file descriptor:
|
311 |
|
312 | echo 'to stderr' >&2
|
313 |
|
314 | <!--
|
315 | NOTE: >&2 is just like <&2
|
316 | There's no real difference.
|
317 | -->
|
318 |
|
319 | ### here-doc
|
320 |
|
321 | TODO: unbalanced HTML if we use \<\<?
|
322 |
|
323 | cat <<EOF
|
324 | here doc with $double ${quoted} substitution
|
325 | EOF
|
326 |
|
327 | myfunc() {
|
328 | cat <<-EOF
|
329 | here doc with one tab leading tab stripped
|
330 | EOF
|
331 | }
|
332 |
|
333 | cat <<< 'here string'
|
334 |
|
335 | <!-- TODO: delimiter can be quoted -->
|
336 | <!-- Note: Python's HTML parser thinks <EOF starts a tag -->
|
337 |
|
338 | ## Other Command
|
339 |
|
340 | <h3 id="dparen" class="osh-topic">dparen ((</h3>
|
341 |
|
342 | <h3 id="time" class="osh-ysh-topic">time</h3>
|
343 |
|
344 | time [-p] pipeline
|
345 |
|
346 | Measures the time taken by a command / pipeline. It uses the `getrusage()`
|
347 | function from `libc`.
|
348 |
|
349 | Note that time is a KEYWORD, not a builtin!
|
350 |
|
351 | <!-- Note: bash respects TIMEFORMAT -->
|
352 |
|
353 |
|
354 | ## YSH Simple
|
355 |
|
356 | ### typed-arg
|
357 |
|
358 | Internal commands (procs and builtins) accept typed arguments.
|
359 |
|
360 | json write (myobj)
|
361 |
|
362 | Block literals have a special syntax:
|
363 |
|
364 | cd /tmp {
|
365 | echo $PWD
|
366 | }
|
367 |
|
368 | This is equivalent to:
|
369 |
|
370 | var cmd = ^(echo $PWD) # unevaluated command
|
371 |
|
372 | cd /tmp (cmd) # pass typed arg
|
373 |
|
374 | ### lazy-expr-arg
|
375 |
|
376 | Expressions in brackets like this:
|
377 |
|
378 | assert [42 === x]
|
379 |
|
380 | Are syntactic sugar for:
|
381 |
|
382 | assert (^[42 === x])
|
383 |
|
384 | That is, it's single arg of type `value.Expr`.
|
385 |
|
386 | ### block-arg
|
387 |
|
388 | Blocks can be passed to builtins (and procs eventually):
|
389 |
|
390 | cd /tmp {
|
391 | echo $PWD # prints /tmp
|
392 | }
|
393 | echo $PWD
|
394 |
|
395 | Compare with [sh-block]($osh-help).
|
396 |
|
397 |
|
398 | ## YSH Assign
|
399 |
|
400 | ### const
|
401 |
|
402 | Binds a name to a YSH expression on the right, with a **dynamic** check to
|
403 | prevent mutation.
|
404 |
|
405 | const c = 'mystr' # equivalent to readonly c=mystr
|
406 | const pat = / digit+ / # an eggex, with no shell equivalent
|
407 |
|
408 | If you try to re-declare or mutate the name, the shell will fail with a runtime
|
409 | error. `const` uses the same mechanism as the `readonly` builtin.
|
410 |
|
411 | Consts should only appear at the top-level, and can't appear within `proc` or
|
412 | `func`.
|
413 |
|
414 | ### var
|
415 |
|
416 | Initializes a name to a YSH expression.
|
417 |
|
418 | var s = 'mystr' # equivalent to declare s=mystr
|
419 | var pat = / digit+ / # an eggex, with no shell equivalent
|
420 |
|
421 | It's either global or scoped to the current function.
|
422 |
|
423 | You can bind multiple variables:
|
424 |
|
425 | var flag, i = parseArgs(spec, ARGV)
|
426 |
|
427 | var x, y = 42, 43
|
428 |
|
429 | You can omit the right-hand side:
|
430 |
|
431 | var x, y # implicitly initialized to null
|
432 |
|
433 | ### setvar
|
434 |
|
435 | At the top-level, setvar creates or mutates a variable.
|
436 |
|
437 | setvar gFoo = 'mutable'
|
438 |
|
439 | Inside a func or proc, it mutates a local variable declared with var.
|
440 |
|
441 | proc p {
|
442 | var x = 42
|
443 | setvar x = 43
|
444 | }
|
445 |
|
446 | You can mutate a List location:
|
447 |
|
448 | setvar a[42] = 'foo'
|
449 |
|
450 | Or a Dict location:
|
451 |
|
452 | setvar d['key'] = 43
|
453 | setvar d.key = 43 # same thing
|
454 |
|
455 | You can use any of these these augmented assignment operators
|
456 |
|
457 | += -= *= /= **= //= %=
|
458 | &= |= ^= <<= >>=
|
459 |
|
460 | Examples:
|
461 |
|
462 | setvar x += 2 # increment by 2
|
463 |
|
464 | setvar a[42] *= 2 # multiply by 2
|
465 |
|
466 | setvar d.flags |= 0b0010_000 # set a flag
|
467 |
|
468 |
|
469 | ### setglobal
|
470 |
|
471 | Creates or mutates a global variable. Has the same syntax as `setvar`.
|
472 |
|
473 |
|
474 | ## YSH Expr
|
475 |
|
476 | ### equal
|
477 |
|
478 | The `=` keyword evaluates an expression and shows the result:
|
479 |
|
480 | oil$ = 1 + 2*3
|
481 | (Int) 7
|
482 |
|
483 | It's meant to be used interactively. Think of it as an assignment with no
|
484 | variable on the left.
|
485 |
|
486 | ### call
|
487 |
|
488 | The `call` keyword evaluates an expression and throws away the result:
|
489 |
|
490 | var x = :| one two |
|
491 | call x->append('three')
|
492 | call x->append(['typed', 'data'])
|
493 |
|
494 |
|
495 | ## YSH Code
|
496 |
|
497 | ### proc-def
|
498 |
|
499 | Procs are shell-like functions, but with named parameters, and without dynamic
|
500 | scope (TODO):
|
501 |
|
502 | proc copy(src, dest) {
|
503 | cp --verbose --verbose $src $dest
|
504 | }
|
505 |
|
506 | Compare with [sh-func]($osh-help).
|
507 |
|
508 | ### func-def
|
509 |
|
510 | TODO
|
511 |
|
512 | ### ysh-return
|
513 |
|
514 | To return an expression, wrap it in `()` as usual:
|
515 |
|
516 | func inc(x) {
|
517 | return (x + 1)
|
518 | }
|
519 |
|
520 | ## YSH Cond
|
521 |
|
522 | ### ysh-if
|
523 |
|
524 | Like shell, you can use a command:
|
525 |
|
526 | if test --file $x {
|
527 | echo "$x is a file"
|
528 | }
|
529 |
|
530 | You can also use an expression:
|
531 |
|
532 | if (x > 0) {
|
533 | echo 'positive'
|
534 | }
|
535 |
|
536 | ### ysh-case
|
537 |
|
538 | Like the shell case statement, the Ysh case statement has **string/glob** patterns.
|
539 |
|
540 | var s = 'README.md'
|
541 | case (s) {
|
542 | *.py { echo 'Python' }
|
543 | *.cc | *.h { echo 'C++' }
|
544 | * { echo 'Other' }
|
545 | }
|
546 | # => Other
|
547 |
|
548 | We also generated it to **typed data** within `()`:
|
549 |
|
550 | var x = 43
|
551 | case (x) {
|
552 | (30 + 12) { echo 'the integer 42' }
|
553 | (else) { echo 'neither' }
|
554 | }
|
555 | # => neither
|
556 |
|
557 | The `else` is a special keyword that matches any value.
|
558 |
|
559 | case (s) {
|
560 | / dot* '.md' / { echo 'Markdown' }
|
561 | (else) { echo 'neither' }
|
562 | }
|
563 | # => Markdown
|
564 |
|
565 | ## YSH Iter
|
566 |
|
567 | ### ysh-while
|
568 |
|
569 | Command or expression:
|
570 |
|
571 | var x = 5
|
572 | while (x < 0) {
|
573 | setvar x -= 1
|
574 | }
|
575 |
|
576 | ### ysh-for
|
577 |
|
578 | Two forms for shell-style loops:
|
579 |
|
580 | for name in *.py {
|
581 | echo "$name"
|
582 | }
|
583 |
|
584 | for i, name in *.py {
|
585 | echo "$i $name"
|
586 | }
|
587 |
|
588 | Two forms for expressions that evaluate to a `List`:
|
589 |
|
590 | for item in (mylist) {
|
591 | echo "$item"
|
592 | }
|
593 |
|
594 | for i, item in (mylist) {
|
595 | echo "$i $item"
|
596 | }
|
597 |
|
598 | Three forms for expressions that evaluate to a `Dict`:
|
599 |
|
600 | for key in (mydict) {
|
601 | echo "$key"
|
602 | }
|
603 |
|
604 | for key, value in (mydict) {
|
605 | echo "$key $value"
|
606 | }
|
607 |
|
608 | for i, key, value in (mydict) {
|
609 | echo "$i $key $value"
|
610 | }
|
611 |
|
612 | # vim: sw=2
|