OILS / doc / ref / toc-ysh.md View on Github | oils.pub

418 lines, 364 significant
1---
2title: YSH Table of Contents
3all_docs_url: ..
4css_files: ../../web/base.css ../../web/manual.css ../../web/ref-index.css
5preserve_anchor_case: yes
6---
7
8<div class="doc-ref-header">
9
10[Oils Reference](index.html) &mdash;
11[OSH](toc-osh.html) | **YSH Table of Contents** | [Data Notation](toc-data.html)
12
13</div>
14
15[YSH]($xref) is shell with a familiar syntax, JSON-like data structures, good
16error handling, and more.
17
18<!--
19<div class="custom-toc">
20
21[type-method](#type-method) &nbsp;
22[builtin-func](#builtin-func) &nbsp;
23[builtin-cmd](#builtin-cmd) &nbsp;
24[front-end](#front-end) &nbsp;
25[cmd-lang](#cmd-lang) &nbsp;
26[ysh-cmd](#ysh-cmd) &nbsp;
27[expr-lang](#expr-lang) &nbsp;
28[word-lang](#word-lang) &nbsp;
29[mini-lang](#mini-lang) &nbsp;
30[option](#option) &nbsp;
31[special-var](#special-var) &nbsp;
32[plugin](#plugin)
33
34</div>
35-->
36
37<h2 id="type-method">
38 Types and Methods <a class="group-link" href="chap-type-method.html">type-method</a>
39</h2>
40
41```chapter-links-type-method
42 [Atoms] Null null
43 Bool expr/true expr/false
44 [Numbers] Int
45 Float
46 Range
47 [String] Str X find() X findLast()
48 X contains() replace()
49 trim() trimStart() trimEnd()
50 startsWith() endsWith()
51 upper() lower()
52 search() leftMatch()
53 split() lines()
54 [Patterns] Eggex
55 Match group() start() end()
56 X groups() X groupDict()
57 [Containers] List List/append() pop() extend()
58 indexOf() lastIndexOf() X includes()
59 insert() remove()
60 reverse() List/clear()
61 Dict erase() X Dict/clear() X accum()
62 X update()
63 Place setValue()
64 [Code Types] Func
65 BuiltinFunc
66 BoundFunc
67 Proc docComment()
68 BuiltinProc
69 [Objects] Obj __invoke__ new
70 X __call__ __index__ X __str__
71 [Reflection] Command sourceCode()
72 Expr
73 Frame
74 DebugFrame toString()
75 io stdin io/eval() io/evalExpr()
76 captureStdout() captureAll() promptVal()
77 X time() X strftime() X glob()
78 vm getFrame() getDebugStack() id()
79```
80
81<h2 id="builtin-func">
82 Builtin Functions <a class="group-link" href="chap-builtin-func.html">builtin-func</a>
83</h2>
84
85```chapter-links-builtin-func
86 [Values] len() func/type()
87 [Conversions] bool() int() float()
88 str() list() dict()
89 X runes() X encodeRunes()
90 X bytes() X encodeBytes()
91 [Str] X strcmp() shSplit()
92 [List] join()
93 [Dict] keys() values() get()
94 [Float] floatsEqual() X isinf() X isnan()
95 [Obj] first() rest() get()
96 [Word] glob() maybe()
97 [Serialize] toJson() fromJson()
98 toJson8() fromJson8()
99 X toJ8Line() X fromJ8Line()
100 [Pattern] _group() _start() _end()
101 [Reflection] func/eval() func/evalExpr()
102 [Introspect] shvarGet() getVar() setVar()
103 parseCommand() X parseExpr() X bindFrame()
104 [Hay Config] parseHay() evalHay()
105X [Hashing] sha1dc() sha256()
106```
107
108<!-- ideas
109More quoting:
110
111quoteC() quotePython() - can we make these ALWAYS work?
112- They can be like J8 b'' strings?
113
114quoteMake() quoteNinja()
115
116quoteSql() - doubling up ''
117quoteCsv() - doubling up ""
118
119X [Wok] _field()
120-->
121
122<h2 id="builtin-cmd">
123 Builtin Commands <a class="group-link" href="chap-builtin-cmd.html">builtin-cmd</a>
124</h2>
125
126<!-- linkify_stop_col is 42 -->
127
128```chapter-links-builtin-cmd_42
129 [Memory] cmd/append Add elements to end of array
130 pp value proc test_
131 asdl_ cell_ X gc-stats_
132 [Handle Errors] error error 'failed' (status=2)
133 try Run with errexit, set _error
134 failed Test if _error.code !== 0
135 boolstatus Enforce 0 or 1 exit status
136 assert assert [42 === f(x)]
137 [Shell State] ysh-cd ysh-shopt compatible, and takes a block
138 shvar Temporary modify global settings
139 ctx Share and update a temporary "context"
140 push-registers Save registers like $?, PIPESTATUS
141 [Word Lookup] invoke Run a command, and control name lookup
142 runproc Run a proc; use as main entry point
143 X extern Run an external command, with an ENV
144 [Modules]
145 source-guard guard against duplicate 'source'
146 is-main false when sourcing a file
147 use create a module Obj from a source file
148 [I/O] ysh-read flags --all, -0
149 ysh-echo no -e -n with simple_echo
150 ysh-test --file --true etc.
151 ysh-wait wait --all --verbose
152 write Like echo, with --, --sep, --end
153 fork forkwait Replace & and (), and takes a block
154 fopen Open multiple streams, takes a block
155 [Private] cat rm POSIX-compatible
156 sleep
157 [Hay Config] hay haynode For DSLs and config files
158 [Completion] compadjust compexport
159 [Data Formats] json read write
160 json8 read write
161```
162
163<h2 id="stdlib">
164 Standard Library<a class="group-link" href="chap-stdlib.html">stdlib</a>
165</h2>
166
167```chapter-links-stdlib
168 [math] abs() max() min() X round()
169 sum()
170 [list] all() any() repeat()
171 [yblocks] yb-capture yb-capture-2
172 [args] parser flag arg rest
173 parseArgs()
174 [binascii] X toBase16() X fromBase16()
175 X toBase64() X fromBase64()
176```
177
178<!-- linkify_stop_col is 42 -->
179
180Design for streams and tables (awk/xargs/dplyr):
181
182```chapter-links-stdlib_42
183X [Lines] slurp-by combine adjacent lines into cells
184X [Awk] each-line --j8 --max-jobs (Str, Template, Block)
185 each-row --max-jobs (Str, Template, Block)
186 each-word xargs-like splitting, similar to IFS too
187 split-by (str=\n, ifs=':', pattern=/s+/)
188 if-split-by only lines that match
189 chop alias for split-by (pattern=/s+/)
190 must-match (/ <capture d+> </capture w+> /)
191 if-match only lines that match
192X [Table Create] table def &place or print TSV8
193 table parse --by-row --by-col (&place), TSV or TSV8
194 table/cols cols name age, or name:Str age:Int
195 types type Str Int
196 attr attr units - secs
197 row emit row
198 table cat concatenate TSV8
199 table align to ssv8
200 table tabify to tsv8 (similar to table parse)
201 table header cols = :|name age|, types = :|Str Int|, ...
202 table slice e.g. slice (1, -1) slice (5, 7)
203 table to-tsv lose type info, and error on \t in cells
204X [Table Ops] where subset of rows; dplyr filter()
205 pick subset of columns ('select' taken by shell)
206 mutate [average = count / sum]
207 transmute drop columns that are used
208 rename (bytes='bytes', path='filename')
209 group-by add a column with a group ID [ext]
210 sort-by sort by columns; dplyr arrange() [ext]
211 summary count/sum, histogram, any/all, reduce, ...
212```
213
214<!--
215Naming ideas:
216
217X [External Lang] BEGIN END when (awk)
218 rule (make) each (xargs) fs (find)
219-->
220
221<h2 id="front-end">
222 Front End <a class="group-link" href="chap-front-end.html">front-end</a>
223</h2>
224
225```chapter-links-front-end
226 [Usage] oils-usage ysh-usage shell-flags
227 config startup line-editing
228 exit-codes
229 [Lexing] comment # line-continuation \ ascii-whitespace [ \t\r\n]
230 [Lexing] doc-comment ### multiline-command ...
231 [Tools] cat-em syntax-tree
232```
233
234<h2 id="cmd-lang">
235 Command Language <a class="group-link" href="chap-cmd-lang.html">cmd-lang</a>
236</h2>
237
238<!-- linkify_stop_col is 33 -->
239
240```chapter-links-cmd-lang_33
241 [Commands] simple-command
242 command-lookup-order
243 ysh-prefix-binding
244 semicolon ;
245 [Redirects] ysh-here-str read <<< '''
246 [YSH Simple] typed-arg json write (x)
247 lazy-expr-arg assert [42 === x]
248 block-arg cd /tmp { echo $PWD }; cd /tmp (; ; blockexpr)
249 [YSH Cond] ysh-case case (x) { *.py { echo 'python' } }
250 ysh-if if (x > 0) { echo }
251 [YSH Iter] ysh-for for i, item in (mylist) { echo }
252 ysh-while while (x > 0) { echo }
253```
254
255<!-- TODO: move YSH command topics to the chapter below -->
256
257<h2 id="ysh-cmd">
258 YSH Command Language Keywords <a class="group-link" href="chap-ysh-cmd.html">ysh-cmd</a>
259</h2>
260
261```chapter-links-ysh-cmd_33
262 [Assignment] const var Declare variables
263 setvar setvar a[i] = 42
264 setglobal setglobal d.key = 'foo'
265 [Expression] equal = = 1 + 2*3
266 call call mylist->append(42)
267 [Definitions] proc proc p (s, ...rest) {
268 typed proc p (; typed, ...rest; n=0; b) {
269 func func f(x; opt1, opt2) { return (x + 1) }
270 ysh-return return (myexpr)
271```
272
273<h2 id="expr-lang">
274 Expression Language and Assignments <a class="group-link" href="chap-expr-lang.html">expr-lang</a>
275</h2>
276
277<!-- linkify_stop_col is 33 -->
278
279```chapter-links-expr-lang_33
280 [Assignment] assign =
281 aug-assign += -= *= /= **= //= %=
282 &= |= ^= <<= >>=
283 [Literals] atom-literal null true false
284 int-literal 42 65_536 0xFF 0o755 0b10
285 float-literal 3.14 1.5e-10
286 X num-suffix 42 K Ki M Mi G Gi T Ti / ms us
287 char-literal \\ \t \" \y00 \u{3bc}
288 ysh-string "x is $x" $"x is $x" r'[a-z]\n'
289 u'line\n' b'byte \yff'
290 triple-quoted """ $""" r''' u''' b'''
291 list-literal ['one', 'two', 3] :| unquoted words |
292 dict-literal {name: 'bob'} {a, b}
293 range 1 ..< n 1 ..= n
294 block-expr ^(echo $PWD)
295 expr-literal ^[1 + 2*3]
296 str-template ^"$a and $b" for Str.replace()
297 X expr-sub $[myobj]
298 X expr-splice @[myobj]
299 [Operators] op-precedence Like Python
300 concat s1 ++ s2, L1 ++ L2
301 ysh-equals === !== ~== is, is not
302 ysh-in in, not in
303 ysh-compare < <= > >= (numbers only)
304 ysh-logical not and or
305 ysh-arith + - * / // % **
306 ysh-bitwise ~ & | ^ << >>
307 ysh-ternary '+' if x >= 0 else '-'
308 ysh-index s[0] mylist[3] mydict['key']
309 ysh-attr mydict.key mystr.startsWith('x')
310 ysh-slice a[1:-1] s[1:-1]
311 ysh-func-call f(x, y, ...pos; n=1, ...named)
312 thin-arrow mylist->pop()
313 fat-arrow mylist => join() => upper()
314 match-ops ~ !~ ~~ !~~
315 [Eggex] re-literal / d+ ; re-flags ; ERE /
316 re-primitive %zero 'sq'
317 class-literal [c a-z 'abc' @str_var \\ \xFF \u{3bc}]
318 named-class dot digit space word d s w
319 re-repeat d? d* d+ d{3} d{2,4}
320 re-compound seq1 seq2 alt1|alt2 (expr1 expr2)
321 re-capture <capture d+ as name: int>
322 re-splice Subpattern @subpattern
323 re-flags reg_icase reg_newline
324 X re-multiline ///
325```
326
327<h2 id="word-lang">
328 Word Language <a class="group-link" href="chap-word-lang.html">word-lang</a>
329</h2>
330
331<!-- linkify_stop_col is 34 -->
332
333```chapter-links-word-lang_34
334 [Joining] ysh-word-join --flag='value' PATH="value"
335 [Quotes] ysh-string "x is $x" $"x is $x" r'[a-z]\n'
336 u'line\n' b'byte \yff'
337 triple-quoted """ $""" r''' u''' b'''
338 X tagged-str "<span id=$x>"html
339 [Substitutions] expr-sub echo $[42 + a[i]]
340 expr-splice echo @[split(x)]
341 var-splice @myarray @ARGV
342 command-sub $(date)
343 ysh-command-sub @(cat my-j8-lines.txt)
344 [Formatting] X ysh-printf ${x %.3f}
345 X ysh-format ${x|html}
346```
347
348<h2 id="mini-lang">
349 Other Mini Languages <a class="group-link" href="chap-mini-lang.html">mini-lang</a>
350</h2>
351
352<!-- linkify_stop_col is 33 -->
353
354```chapter-links-mini-lang_33
355 [Patterns] glob-pat *.py
356 [Other Sublang] braces {alice,bob}@example.com
357```
358
359<h2 id="option">
360 Global Shell Options <a class="group-link" href="chap-option.html">option</a>
361</h2>
362
363```chapter-links-option
364 [Optimize] rewrite_extern
365 [Groups] strict:all ysh:upgrade ysh:all
366 [YSH Details] opts-redefine opts-internal
367```
368
369<h2 id="special-var">
370 Special Variables <a class="group-link" href="chap-special-var.html">special-var</a>
371</h2>
372
373```chapter-links-special-var
374 [YSH Vars] ARGV ENV
375 __defaults__ __builtins__ X __sh_function__
376 _this_dir
377 [YSH Status] _error
378 _pipeline_status _process_sub_status
379 [YSH Tracing] SHX_indent SHX_punct SHX_pid_str
380 [YSH read] _reply
381 [History] YSH_HISTFILE
382 [Interactive] OILS_COMP_UI
383 [Oils VM] OILS_VERSION
384 OILS_GC_THRESHOLD OILS_GC_ON_EXIT
385 OILS_GC_STATS OILS_GC_STATS_FD
386 LIB_YSH
387 [Float] NAN INFINITY
388 [Module] __provide__
389 [Other Env] HOME PATH
390```
391
392<!--
393ENV vars read by interpreter:
394
395ENV.{PS1,PS4,YSH_HISTFILE}
396
397- renderPrompt() takes precedence over PS1
398- SHX_* takes precedence over PS4
399 - TODO: we may want to redo this - it is too confusing
400- HOME is read for ~, but it is not SET
401 - we should read ENV.HOME
402 - should be populate ENV.HOME?
403
404Notes:
405 [Module] __E__ - for closures
406X [Builtin Sub] _buffer
407
408Ideas:
409X [Wok] _filename _line _line_num
410-->
411
412<h2 id="plugin">
413 Plugins and Hooks <a class="group-link" href="chap-plugin.html">plugin</a>
414</h2>
415
416```chapter-links-plugin
417 [YSH] renderPrompt()
418```