OILS / doc / ref / toc-ysh.md View on Github | oilshell.org

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