OILS / doc / ref / chap-word-lang.md View on Github | oilshell.org

148 lines, 86 significant
1---
2title: Word Language (Oils Reference)
3all_docs_url: ..
4body_css_class: width40
5default_highlighter: oils-sh
6preserve_anchor_case: yes
7---
8
9<div class="doc-ref-header">
10
11[Oils Reference](index.html) &mdash;
12Chapter **Word Language**
13
14</div>
15
16This chapter describes the word language for OSH and YSH. Words evaluate to
17strings, or arrays of strings.
18
19<span class="in-progress">(in progress)</span>
20
21<div id="dense-toc">
22</div>
23
24<h2 id="expression">Expressions to Words</h2>
25
26### expr-sub
27
28### expr-splice
29
30### var-splice
31
32<h2 id="formatting">Formatting Typed Data as Strings</h2>
33
34### ysh-printf
35
36### ysh-format
37
38
39## Quotes
40
41### osh-string
42
43- Single quotes
44- Double Quotes
45- C-style strings: `$'\n'`
46
47TODO: elaborate
48
49### ysh-string
50
51YSH strings in the word language are the same as in the expression language.
52
53See [ysh-string in chap-expr-lang](chap-expr-lang.html#ysh-string).
54
55### triple-quoted
56
57Triple-quoted in the word language are the same as in the expression language.
58
59See [triple-quoted in chap-expr-lang](chap-expr-lang.html#triple-quoted).
60
61### tagged-str
62
63Not done.
64
65## Substitutions
66
67### command-sub
68
69Executes a command and captures its stdout.
70
71OSH has shell-compatible command sub like `$(echo hi)`. If a trailing newline
72is returned, it's removed:
73
74 $ hostname
75 example.com
76
77 $ echo "/tmp/$(hostname)"
78 /tmp/example.com
79
80YSH has spliced command subs, enabled by `shopt --set parse_at`. The reuslt is
81a **List** of strings, rather than a single string.
82
83 $ write -- @(echo foo; echo 'with spaces')
84 foo
85 with-spaces
86
87The command's stdout parsed as the "J8 Lines" format, where each line is
88either:
89
901. An unquoted string, which must be valid UTF-8. Whitespace is allowed, but
91 not other ASCII control chars.
922. A quoted J8 string (JSON style `""` or J8-style `b'' u'' ''`)
933. An **ignored** empty line
94
95See [J8 Notation](../j8-notation.html) for more details.
96
97### var-sub
98
99Evaluates to the value of a variable:
100
101 $ x=X
102 $ echo $x ${x}
103 X X
104
105### arith-sub
106
107Shell has C-style arithmetic:
108
109 $ echo $(( 1 + 2*3 ))
110 7
111
112### tilde-sub
113
114Used as a shortcut for a user's home directory:
115
116 ~/src # my home dir
117 ~bob/src # user bob's home dir
118
119### proc-sub
120
121Open stdout as a named file in `/dev/fd`, which can be passed to a command:
122
123 diff <(sort L.txt) <(sort R.txt)
124
125Open stdin as a named file in `/dev/fd`:
126
127 seq 3 | tee >(sleep 1; tac)
128
129
130## Var Ops
131
132### op-test
133
134### op-strip
135
136### op-replace
137
138### op-index
139
140 ${a[i+1]}
141
142### op-slice
143
144### op-format
145
146${x@P} evaluates x as a prompt string, e.g. the string that would be printed if
147PS1=$x.
148