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

407 lines, 353 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 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
69 CommandFrag
70 Expr
71 Frame
72 DebugFrame toString()
73 io stdin evalExpr()
74 eval() evalToDict()
75 captureStdout()
76 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()
100X [Quoting] quoteJ8() quoteSh() quoteHtml()
101 [Pattern] _group() _start() _end()
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 [Introspection] runproc Run a proc; use as main entry point
142 X extern Run an external command, with an ENV
143 X invoke Control which "invokables" are run
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 write Like echo, with --, --sep, --end
152 fork forkwait Replace & and (), and takes a block
153 fopen Open multiple streams, takes a block
154 [Hay Config] hay haynode For DSLs and config files
155 [Completion] compadjust compexport
156 [Data Formats] json read write
157 json8 read write
158```
159
160<h2 id="stdlib">
161 Standard Library<a class="group-link" href="chap-stdlib.html">stdlib</a>
162</h2>
163
164```chapter-links-stdlib
165 [math] abs() max() min() X round()
166 sum()
167 [list] all() any() repeat()
168 [yblocks] yb-capture yb-capture-2
169 [args] parser flag arg rest
170 parseArgs()
171```
172
173<!-- linkify_stop_col is 42 -->
174
175Design for streams and tables (awk/xargs/dplyr):
176
177```chapter-links-stdlib_42
178X [Lines] slurp-by combine adjacent lines into cells
179X [Awk] each-line --j8 --max-jobs (Str, Template, Block)
180 each-row --max-jobs (Str, Template, Block)
181 each-word xargs-like splitting, similar to IFS too
182 split-by (str=\n, ifs=':', pattern=/s+/)
183 if-split-by only lines that match
184 chop alias for split-by (pattern=/s+/)
185 must-match (/ <capture d+> </capture w+> /)
186 if-match only lines that match
187X [Table Create] table def &place or print TSV8
188 table parse --by-row --by-col (&place), TSV or TSV8
189 table/cols cols name age, or name:Str age:Int
190 types type Str Int
191 attr attr units - secs
192 row emit row
193 table cat concatenate TSV8
194 table align to ssv8
195 table tabify to tsv8 (similar to table parse)
196 table header cols = :|name age|, types = :|Str Int|, ...
197 table slice e.g. slice (1, -1) slice (5, 7)
198 table to-tsv lose type info, and error on \t in cells
199X [Table Ops] where subset of rows; dplyr filter()
200 pick subset of columns ('select' taken by shell)
201 mutate [average = count / sum]
202 transmute drop columns that are used
203 rename (bytes='bytes', path='filename')
204 group-by add a column with a group ID [ext]
205 sort-by sort by columns; dplyr arrange() [ext]
206 summary count/sum, histogram, any/all, reduce, ...
207```
208
209<!--
210Naming ideas:
211
212X [External Lang] BEGIN END when (awk)
213 rule (make) each (xargs) fs (find)
214-->
215
216<h2 id="front-end">
217 Front End <a class="group-link" href="chap-front-end.html">front-end</a>
218</h2>
219
220```chapter-links-front-end
221 [Usage] oils-usage ysh-usage
222 [Lexing] ascii-whitespace [ \t\r\n]
223 doc-comment ### multiline-command ...
224 [Tools] cat-em
225```
226
227<h2 id="cmd-lang">
228 Command Language <a class="group-link" href="chap-cmd-lang.html">cmd-lang</a>
229</h2>
230
231<!-- linkify_stop_col is 33 -->
232
233```chapter-links-cmd-lang_33
234 [Commands] simple-command
235 ysh-prefix-binding
236 semicolon ;
237 [Redirects] ysh-here-str read <<< '''
238 [YSH Simple] typed-arg json write (x)
239 lazy-expr-arg assert [42 === x]
240 block-arg cd /tmp { echo $PWD }; cd /tmp (; ; blockexpr)
241 [YSH Cond] ysh-case case (x) { *.py { echo 'python' } }
242 ysh-if if (x > 0) { echo }
243 [YSH Iter] ysh-for for i, item in (mylist) { echo }
244 ysh-while while (x > 0) { echo }
245```
246
247<!-- TODO: move YSH command topics to the chapter below -->
248
249<h2 id="ysh-cmd">
250 YSH Command Language Keywords <a class="group-link" href="chap-ysh-cmd.html">ysh-cmd</a>
251</h2>
252
253```chapter-links-ysh-cmd_39
254 [Assignment] const var Declare variables
255 setvar setvar a[i] = 42
256 setglobal setglobal d.key = 'foo'
257 [Expression] equal = = 1 + 2*3
258 call call mylist->append(42)
259 [Definitions] proc proc p (s, ...rest) {
260 typed proc p (; typed, ...rest; n=0; b) {
261 func func f(x; opt1, opt2) { return (x + 1) }
262 ysh-return return (myexpr)
263```
264
265<h2 id="expr-lang">
266 Expression Language and Assignments <a class="group-link" href="chap-expr-lang.html">expr-lang</a>
267</h2>
268
269<!-- linkify_stop_col is 33 -->
270
271```chapter-links-expr-lang_33
272 [Assignment] assign =
273 aug-assign += -= *= /= **= //= %=
274 &= |= ^= <<= >>=
275 [Literals] atom-literal null true false
276 int-literal 42 65_536 0xFF 0o755 0b10
277 float-literal 3.14 1.5e-10
278 X num-suffix 42 K Ki M Mi G Gi T Ti / ms us
279 char-literal \\ \t \" \y00 \u{3bc}
280 ysh-string "x is $x" $"x is $x" r'[a-z]\n'
281 u'line\n' b'byte \yff'
282 triple-quoted """ $""" r''' u''' b'''
283 list-literal ['one', 'two', 3] :| unquoted words |
284 dict-literal {name: 'bob'} {a, b}
285 range 1 ..< n 1 ..= n
286 block-expr ^(echo $PWD)
287 expr-literal ^[1 + 2*3]
288 str-template ^"$a and $b" for Str.replace()
289 X expr-sub $[myobj]
290 X expr-splice @[myobj]
291 [Operators] op-precedence Like Python
292 concat s1 ++ s2, L1 ++ L2
293 ysh-equals === !== ~== is, is not
294 ysh-in in, not in
295 ysh-compare < <= > >= (numbers only)
296 ysh-logical not and or
297 ysh-arith + - * / // % **
298 ysh-bitwise ~ & | ^ << >>
299 ysh-ternary '+' if x >= 0 else '-'
300 ysh-index s[0] mylist[3] mydict['key']
301 ysh-attr mydict.key mystr.startsWith('x')
302 ysh-slice a[1:-1] s[1:-1]
303 ysh-func-call f(x, y, ...pos; n=1, ...named)
304 thin-arrow mylist->pop()
305 fat-arrow mylist => join() => upper()
306 match-ops ~ !~ ~~ !~~
307 [Eggex] re-literal / d+ ; re-flags ; ERE /
308 re-primitive %zero 'sq'
309 class-literal [c a-z 'abc' @str_var \\ \xFF \u{3bc}]
310 named-class dot digit space word d s w
311 re-repeat d? d* d+ d{3} d{2,4}
312 re-compound seq1 seq2 alt1|alt2 (expr1 expr2)
313 re-capture <capture d+ as name: int>
314 re-splice Subpattern @subpattern
315 re-flags reg_icase reg_newline
316 X re-multiline ///
317```
318
319<h2 id="word-lang">
320 Word Language <a class="group-link" href="chap-word-lang.html">word-lang</a>
321</h2>
322
323<!-- linkify_stop_col is 33 -->
324
325```chapter-links-word-lang_33
326 [Quotes] ysh-string "x is $x" $"x is $x" r'[a-z]\n'
327 u'line\n' b'byte \yff'
328 triple-quoted """ $""" r''' u''' b'''
329 X tagged-str "<span id=$x>"html
330 [Substitutions] expr-sub echo $[42 + a[i]]
331 expr-splice echo @[split(x)]
332 var-splice @myarray @ARGV
333 command-sub @(cat my-j8-lines.txt)
334 [Formatting] X ysh-printf ${x %.3f}
335 X ysh-format ${x|html}
336```
337
338<h2 id="mini-lang">
339 Other Mini Languages <a class="group-link" href="chap-mini-lang.html">mini-lang</a>
340</h2>
341
342<!-- linkify_stop_col is 33 -->
343
344```chapter-links-mini-lang_33
345 [Patterns] glob-pat *.py
346 [Other Sublang] braces {alice,bob}@example.com
347```
348
349<h2 id="option">
350 Global Shell Options <a class="group-link" href="chap-option.html">option</a>
351</h2>
352
353```chapter-links-option
354 [Groups] strict:all ysh:upgrade ysh:all
355 [YSH Details] opts-redefine opts-internal
356```
357
358<h2 id="special-var">
359 Special Variables <a class="group-link" href="chap-special-var.html">special-var</a>
360</h2>
361
362```chapter-links-special-var
363 [YSH Vars] ARGV ENV
364 __defaults__ __builtins__
365 _this_dir
366 [YSH Status] _error
367 _pipeline_status _process_sub_status
368 [YSH Tracing] SHX_indent SHX_punct SHX_pid_str
369 [YSH read] _reply
370 [History] YSH_HISTFILE
371 [Interactive] OILS_COMP_UI
372 [Oils VM] OILS_VERSION
373 OILS_GC_THRESHOLD OILS_GC_ON_EXIT
374 OILS_GC_STATS OILS_GC_STATS_FD
375 LIB_YSH
376 [Float] NAN INFINITY
377 [Module] __provide__
378 [Other Env] HOME PATH
379```
380
381<!--
382ENV vars read by interpreter:
383
384ENV.{PS1,PS4,YSH_HISTFILE}
385
386- renderPrompt() takes precedence over PS1
387- SHX_* takes precedence over PS4
388 - TODO: we may want to redo this - it is too confusing
389- HOME is read for ~, but it is not SET
390 - we should read ENV.HOME
391 - should be populate ENV.HOME?
392
393Notes:
394 [Module] __E__ - for evalToDict()?
395X [Builtin Sub] _buffer
396
397Ideas:
398X [Wok] _filename _line _line_num
399-->
400
401<h2 id="plugin">
402 Plugins and Hooks <a class="group-link" href="chap-plugin.html">plugin</a>
403</h2>
404
405```chapter-links-plugin
406 [YSH] renderPrompt()
407```