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

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