| 1 | ---
 | 
| 2 | title: Word Language (Oils Reference)
 | 
| 3 | all_docs_url: ..
 | 
| 4 | body_css_class: width40
 | 
| 5 | default_highlighter: oils-sh
 | 
| 6 | preserve_anchor_case: yes
 | 
| 7 | ---
 | 
| 8 | 
 | 
| 9 | <div class="doc-ref-header">
 | 
| 10 | 
 | 
| 11 | [Oils Reference](index.html) —
 | 
| 12 | Chapter **Word Language**
 | 
| 13 | 
 | 
| 14 | </div>
 | 
| 15 | 
 | 
| 16 | This chapter describes the word language for OSH and YSH.  Words evaluate to
 | 
| 17 | strings, 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 | 
 | 
| 47 | TODO: elaborate
 | 
| 48 | 
 | 
| 49 | ### ysh-string
 | 
| 50 | 
 | 
| 51 | YSH strings in the word language are the same as in the expression language.
 | 
| 52 | 
 | 
| 53 | See [ysh-string in chap-expr-lang](chap-expr-lang.html#ysh-string).
 | 
| 54 | 
 | 
| 55 | ### triple-quoted
 | 
| 56 | 
 | 
| 57 | Triple-quoted in the word language are the same as in the expression language.
 | 
| 58 | 
 | 
| 59 | See [triple-quoted in chap-expr-lang](chap-expr-lang.html#triple-quoted).
 | 
| 60 | 
 | 
| 61 | ### tagged-str
 | 
| 62 | 
 | 
| 63 | Not done.
 | 
| 64 | 
 | 
| 65 | ## Substitutions
 | 
| 66 | 
 | 
| 67 | ### command-sub
 | 
| 68 | 
 | 
| 69 | Executes a command and captures its stdout.
 | 
| 70 | 
 | 
| 71 | OSH has shell-compatible command sub like `$(echo hi)`.  If a trailing newline
 | 
| 72 | is returned, it's removed:
 | 
| 73 | 
 | 
| 74 |     $ hostname
 | 
| 75 |     example.com
 | 
| 76 | 
 | 
| 77 |     $ echo "/tmp/$(hostname)"
 | 
| 78 |     /tmp/example.com
 | 
| 79 | 
 | 
| 80 | YSH has spliced command subs, enabled by `shopt --set parse_at`.  The reuslt is
 | 
| 81 | a **List** of strings, rather than a single string.
 | 
| 82 | 
 | 
| 83 |     $ write -- @(echo foo; echo 'with spaces')
 | 
| 84 |     foo
 | 
| 85 |     with-spaces
 | 
| 86 | 
 | 
| 87 | The command's stdout parsed as the "J8 Lines" format, where each line is
 | 
| 88 | either:
 | 
| 89 | 
 | 
| 90 | 1. An unquoted string, which must be valid UTF-8.  Whitespace is allowed, but
 | 
| 91 |    not other ASCII control chars.
 | 
| 92 | 2. A quoted J8 string (JSON style `""` or J8-style `b'' u'' ''`)
 | 
| 93 | 3. An **ignored** empty line
 | 
| 94 | 
 | 
| 95 | See [J8 Notation](../j8-notation.html) for more details.
 | 
| 96 | 
 | 
| 97 | ### var-sub
 | 
| 98 | 
 | 
| 99 | Evaluates to the value of a variable:
 | 
| 100 | 
 | 
| 101 |     $ x=X
 | 
| 102 |     $ echo $x ${x}
 | 
| 103 |     X X
 | 
| 104 | 
 | 
| 105 | ### arith-sub
 | 
| 106 | 
 | 
| 107 | Shell has C-style arithmetic:
 | 
| 108 | 
 | 
| 109 |     $ echo $(( 1 + 2*3 ))
 | 
| 110 |     7
 | 
| 111 | 
 | 
| 112 | ### tilde-sub
 | 
| 113 | 
 | 
| 114 | Used 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 | 
 | 
| 121 | Open 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 | 
 | 
| 125 | Open 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
 | 
| 147 | PS1=$x.
 | 
| 148 | 
 |