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

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