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

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