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

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