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

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