| 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 | ## Glob
|
| 25 |
|
| 26 | ### osh-glob
|
| 27 |
|
| 28 | Expand a [glob pattern][glob-pat] into arguments.
|
| 29 |
|
| 30 | echo *_test.py # => foo_test.py bar_test.py
|
| 31 |
|
| 32 | Expansion respects these global settings:
|
| 33 |
|
| 34 | - [Globbing options][globbing-options] like `dotglob nullglob no_dash_glob`
|
| 35 | - [GLOBIGNORE][]
|
| 36 | - The `libc` `LC_COLLATE` var (like bash and zsh)
|
| 37 | - Related: [osh-locale][]
|
| 38 |
|
| 39 | Example of collation:
|
| 40 |
|
| 41 | $ touch foo-bar foo_bar
|
| 42 | $ echo foo* # foo_bar MAY come first
|
| 43 |
|
| 44 | [glob-pat]: chap-mini-lang.html#glob-pat
|
| 45 | [GLOBIGNORE]: chap-special-var.html#GLOBIGNORE
|
| 46 | [globbing-options]: chap-option.html#Globbing
|
| 47 | [osh-locale]: chap-special-var.html#osh-locale
|
| 48 |
|
| 49 | ### ysh-glob
|
| 50 |
|
| 51 | Expand a [glob pattern][glob-pat] into arguments.
|
| 52 |
|
| 53 | echo *_test.py # => foo_test.py bar_test.py
|
| 54 |
|
| 55 | In YSH, glob expansion is always done with the default `libc` collation:
|
| 56 |
|
| 57 | ysh$ touch foo-bar foo_bar
|
| 58 | ysh$ echo foo* # foo-bar always comes first
|
| 59 |
|
| 60 | Related: [ysh-locale][], the [glob()][glob] function
|
| 61 |
|
| 62 | [ysh-locale]: chap-special-var.html#ysh-locale
|
| 63 | [glob]: chap-builtin-func.html#glob
|
| 64 |
|
| 65 | <h2 id="expression">Expressions to Words</h2>
|
| 66 |
|
| 67 | ### expr-sub
|
| 68 |
|
| 69 | Turn an expression into a string. Examples:
|
| 70 |
|
| 71 | $ echo $[3 * 2]
|
| 72 | 6
|
| 73 |
|
| 74 | $ var s = 'foo'
|
| 75 | $ echo $[s[1:]]
|
| 76 | oo
|
| 77 |
|
| 78 | Some types can't be stringified, like Dict and List:
|
| 79 |
|
| 80 | $ var d = {k: 42}
|
| 81 |
|
| 82 | $ echo $[d]
|
| 83 | fatal: expected Null, Bool, Int, Float, Eggex
|
| 84 |
|
| 85 | You can explicitly use `toJson8` or `toJson()`:
|
| 86 |
|
| 87 | $ echo $[toJson8(d)]
|
| 88 | {"k":42}
|
| 89 |
|
| 90 | (This is similar to `json write (d)`)
|
| 91 |
|
| 92 | ### expr-splice
|
| 93 |
|
| 94 | Splicing turns each element of a `List` into a string, and puts those strings
|
| 95 | into an array:
|
| 96 |
|
| 97 | $ var foods = ['ale', 'bean', 42]
|
| 98 | $ echo pizza @[foods[1:]] worm
|
| 99 | pizza bean 42 worm
|
| 100 |
|
| 101 | It also works in arays:
|
| 102 |
|
| 103 | $ var myarray = :| prefix @[foods]] |
|
| 104 | $ echo @myarray
|
| 105 | prefix ale bean 42
|
| 106 |
|
| 107 | This syntax is enabled by `shopt --set` [parse_at][], which is part of YSH.
|
| 108 |
|
| 109 | [parse_at]: chap-option.html#ysh:upgrade
|
| 110 |
|
| 111 | ### var-splice
|
| 112 |
|
| 113 | $ var foods = ['ale', 'bean', 'corn']
|
| 114 | echo @foods
|
| 115 |
|
| 116 | This syntax is enabled by `shopt --set` [parse_at][], which is part of YSH.
|
| 117 |
|
| 118 |
|
| 119 | <h2 id="formatting">Formatting Typed Data as Strings</h2>
|
| 120 |
|
| 121 | ### ysh-printf
|
| 122 |
|
| 123 | Not done.
|
| 124 |
|
| 125 | echo ${x %.3f}
|
| 126 |
|
| 127 | ### ysh-format
|
| 128 |
|
| 129 | Not done.
|
| 130 |
|
| 131 | echo ${x|html}
|
| 132 |
|
| 133 | ## Joining
|
| 134 |
|
| 135 | ### osh-word-join
|
| 136 |
|
| 137 | OSH joins arbitrary word parts:
|
| 138 |
|
| 139 | $ myvar=/
|
| 140 | $ echo 'single'\'$myvar"double $myvar"
|
| 141 | single'/double /
|
| 142 |
|
| 143 | That is, the argument to `echo` is a word that has 4 word parts:
|
| 144 |
|
| 145 | 'single'
|
| 146 | \'
|
| 147 | $myvar
|
| 148 | "double $myvar"
|
| 149 |
|
| 150 | When the word is evaluated, the result of each part is evaluated and
|
| 151 | concatenated.
|
| 152 |
|
| 153 | ### ysh-word-join
|
| 154 |
|
| 155 | In general, YSH doesn't allow single- and double-quoted parts to be joined.
|
| 156 | They most often form an entire word:
|
| 157 |
|
| 158 | echo 'single-quoted word'
|
| 159 | echo "double-quoted word"
|
| 160 |
|
| 161 | YSH allows word joining in these special cases:
|
| 162 |
|
| 163 | echo --flag='value'
|
| 164 | echo NAME="value"
|
| 165 |
|
| 166 | echo ~/'dir with spaces'
|
| 167 | echo ~root/src/"dir with spaces"
|
| 168 |
|
| 169 | The purpose of this rule is to eliminate ambiguous words like:
|
| 170 |
|
| 171 | --flag=u'value\n' # does u indicate a J8 string, or is it a literal?
|
| 172 |
|
| 173 | See [OILS-ERR-17][] for details.
|
| 174 |
|
| 175 | [OILS-ERR-17]: ../error-catalog.html#oils-err-17
|
| 176 |
|
| 177 | ## Quotes
|
| 178 |
|
| 179 |
|
| 180 | ### osh-string
|
| 181 |
|
| 182 | - Single quotes
|
| 183 | - Double Quotes
|
| 184 | - C-style strings: `$'\n'`
|
| 185 |
|
| 186 | TODO: elaborate
|
| 187 |
|
| 188 | ### ysh-string
|
| 189 |
|
| 190 | YSH strings in the word language are the same as in the expression language.
|
| 191 |
|
| 192 | See [ysh-string in chap-expr-lang](chap-expr-lang.html#ysh-string).
|
| 193 |
|
| 194 | ### triple-quoted
|
| 195 |
|
| 196 | Triple-quoted in the word language are the same as in the expression language.
|
| 197 |
|
| 198 | See [triple-quoted in chap-expr-lang](chap-expr-lang.html#triple-quoted).
|
| 199 |
|
| 200 | ### tagged-str
|
| 201 |
|
| 202 | Not done.
|
| 203 |
|
| 204 | ## Substitutions
|
| 205 |
|
| 206 | ### command-sub
|
| 207 |
|
| 208 | Executes a command and captures its stdout.
|
| 209 |
|
| 210 | If stdout has a trailing newline, it's removed:
|
| 211 |
|
| 212 | $ hostname
|
| 213 | example.com
|
| 214 |
|
| 215 | $ echo "/tmp/$(hostname)"
|
| 216 | /tmp/example.com
|
| 217 |
|
| 218 | If stdout has any NUL bytes, they are removed (regardless of position).
|
| 219 |
|
| 220 | Related: [captureStdout()](chap-type-method.html#captureStdout)
|
| 221 |
|
| 222 | ### command-splice
|
| 223 |
|
| 224 | YSH also has spliced command subs, enabled by `shopt --set parse_at`. The
|
| 225 | result is a **List** of strings, rather than a single string.
|
| 226 |
|
| 227 | $ write -- @(echo foo; echo 'with spaces')
|
| 228 | foo
|
| 229 | with-spaces
|
| 230 |
|
| 231 | The command's stdout parsed as the "J8 Lines" format, where each line is
|
| 232 | either:
|
| 233 |
|
| 234 | 1. An unquoted string, which must be valid UTF-8. Whitespace is allowed, but
|
| 235 | not other ASCII control chars.
|
| 236 | 2. A quoted J8 string (JSON style `""` or J8-style `b'' u'' ''`)
|
| 237 | 3. An **ignored** empty line
|
| 238 |
|
| 239 | See [J8 Notation](../j8-notation.html) for more details.
|
| 240 |
|
| 241 | ### var-sub
|
| 242 |
|
| 243 | Evaluates to the value of a variable:
|
| 244 |
|
| 245 | $ x=X
|
| 246 | $ echo $x ${x}
|
| 247 | X X
|
| 248 |
|
| 249 | ### arith-sub
|
| 250 |
|
| 251 | Shell has C-style arithmetic:
|
| 252 |
|
| 253 | $ echo $(( 1 + 2*3 ))
|
| 254 | 7
|
| 255 |
|
| 256 | ### tilde-sub
|
| 257 |
|
| 258 | Used as a shortcut for a user's home directory:
|
| 259 |
|
| 260 | ~/src # my home dir
|
| 261 | ~bob/src # user bob's home dir
|
| 262 |
|
| 263 | ### proc-sub
|
| 264 |
|
| 265 | Open stdout as a named file in `/dev/fd`, which can be passed to a command:
|
| 266 |
|
| 267 | diff <(sort L.txt) <(sort R.txt)
|
| 268 |
|
| 269 | Open stdin as a named file in `/dev/fd`:
|
| 270 |
|
| 271 | seq 3 | tee >(sleep 1; tac)
|
| 272 |
|
| 273 |
|
| 274 | ## Var Ops
|
| 275 |
|
| 276 | There are three types of braced variable expansions:
|
| 277 |
|
| 278 | ${!name*} or ${!name@}
|
| 279 | ${!name[@]} or ${!name[*]}
|
| 280 | ${ops var ops}
|
| 281 |
|
| 282 | `name` needs to be a valid identifier. If the expansion matches the first
|
| 283 | form, the variable names starting with `name` are generated. Otherwise, if the
|
| 284 | expansion matches the second form, the keys of the indexed or associative array
|
| 285 | named `name` are generated. When the expansion does not much either the first
|
| 286 | or second forms, it is interpreted as the third form of the variable name
|
| 287 | surrounded by operators.
|
| 288 |
|
| 289 |
|
| 290 | ### op-bracket
|
| 291 |
|
| 292 | The value within brackets is called an "index", and retrieves a value from an
|
| 293 | array:
|
| 294 |
|
| 295 | ${A[i+1]}
|
| 296 | ${A['key']}
|
| 297 |
|
| 298 | If `A` is an indexed array, the index is interpreted as an arithmetic
|
| 299 | expression. Arithmetic evaluation is performed, and the value at that numeric
|
| 300 | offset is retrieved.
|
| 301 |
|
| 302 | If `A` is an associative array, the index is interpreted as a string. The
|
| 303 | value associated with that string is retrieved.
|
| 304 |
|
| 305 | If `A` is a string, it's treated as an indexed array with a single element,
|
| 306 | i.e. so that `${A[0]}` is `${A}`.
|
| 307 |
|
| 308 | ---
|
| 309 |
|
| 310 | ${A[*]}
|
| 311 | ${A[@]}
|
| 312 |
|
| 313 | The index expressions `[*]` and `[@]` are special cases. Both generate a word
|
| 314 | list of all elements in `a`.
|
| 315 |
|
| 316 | When the variable substitution is **unquoted**, there's no difference between
|
| 317 | `[*]` and `[@]`:
|
| 318 |
|
| 319 | $ A=(1 2 3)
|
| 320 | $ printf '<%s>\n' ${A[*]}
|
| 321 | <1>
|
| 322 | <2>
|
| 323 | <3>
|
| 324 |
|
| 325 | $ printf '<%s>\n' ${A[@]}
|
| 326 | <1>
|
| 327 | <2>
|
| 328 | <3>
|
| 329 |
|
| 330 | When double-quoted, the `[*]` form joins the elements by the first character of
|
| 331 | `IFS`:
|
| 332 |
|
| 333 | $ IFS=x
|
| 334 | $ printf '<%s>\n' "${A[*]}"
|
| 335 | <1x2x3>
|
| 336 |
|
| 337 | When double-quoted, the `[@]` form generates a word list by splitting the word
|
| 338 | at the boundary of every element in `A`:
|
| 339 |
|
| 340 | $ printf '<%s>\n' "-${A[@]}-"
|
| 341 | <-1>
|
| 342 | <2>
|
| 343 | <3->
|
| 344 |
|
| 345 | If the container `A` has no elements, and the variable substitution has no
|
| 346 | other parts, `[@]` evaluates to an empty word list:
|
| 347 |
|
| 348 | $ empty=()
|
| 349 | $ set -- "${empty[@]}"
|
| 350 | $ echo $#
|
| 351 | 0
|
| 352 |
|
| 353 | ---
|
| 354 |
|
| 355 | These rules for `[*]` and `[@]` also apply to:
|
| 356 |
|
| 357 | - `$*` and `$@`
|
| 358 | - `${!name*}` and `${!name@}`
|
| 359 | - `${!name[*]}` and `${!name[@]}`, etc.
|
| 360 |
|
| 361 | <!--
|
| 362 | Note: OSH currently joins the values by `IFS` even for unquoted `$*` and
|
| 363 | performs word splitting afterward. This is different from the POSIX standard.
|
| 364 | -->
|
| 365 |
|
| 366 | ### op-indirect
|
| 367 |
|
| 368 | The indirection operator `!` is a prefix operator, and it interprets the
|
| 369 | received string as a variable name `name`, an array element `name[key]`, or an
|
| 370 | arrat list `name[@]` / `name[*]` and reads its values.
|
| 371 |
|
| 372 | $ a=1234
|
| 373 | $ v=a
|
| 374 | $ echo $v
|
| 375 | a
|
| 376 | $ echo ${!v}
|
| 377 | 1234
|
| 378 |
|
| 379 | ### op-test
|
| 380 |
|
| 381 | Shell has boolean operations within `${}`. I use `:-` most frequently:
|
| 382 |
|
| 383 | x=${1:-default}
|
| 384 | osh=${OSH:-default}
|
| 385 |
|
| 386 | This idiom is also useful:
|
| 387 |
|
| 388 | : ${LIB_OSH=stdlib/osh}
|
| 389 |
|
| 390 | ---
|
| 391 |
|
| 392 | There are test operators with colons, and without:
|
| 393 |
|
| 394 | ${x-default}
|
| 395 | ${x:-default}
|
| 396 |
|
| 397 | ${x=default}
|
| 398 | ${x:=default}
|
| 399 |
|
| 400 | ${x+other}
|
| 401 | ${x:+other}
|
| 402 |
|
| 403 | ${x?error}
|
| 404 | ${x:?error}
|
| 405 |
|
| 406 | **Without** the colon, the shell checks whether a value is **defined**. In the
|
| 407 | case of a word list, e.g. generated by `$*` or `$@`, it tests whether there is
|
| 408 | at least one element.
|
| 409 |
|
| 410 | **With** the colon, the shell checks whether the value is **non-empty** (is not
|
| 411 | the empty string). In the case of a word list, the test is performed after
|
| 412 | joining the elements by a space.
|
| 413 |
|
| 414 | Elements are joined by the first character of `IFS` only with double-quoted
|
| 415 | `"${*:-}"`.
|
| 416 |
|
| 417 | In contrast, `${*:-}`, `${@:-}`, and `"${@:-}"` are joined by a space. This is
|
| 418 | because the joining of `"$*"` by `IFS` is performed earlier than the joining by
|
| 419 | space for the test.
|
| 420 |
|
| 421 | <!--
|
| 422 | Note: OSH currently joins the values by `IFS` even for unquoted `$*`. This is
|
| 423 | different from Bash.
|
| 424 | -->
|
| 425 |
|
| 426 | ### op-strip
|
| 427 |
|
| 428 | Remove prefixes or suffixes from strings:
|
| 429 |
|
| 430 | echo ${y#prefix}
|
| 431 | echo ${y##'prefix'}
|
| 432 |
|
| 433 | echo ${y%suffix}
|
| 434 | echo ${y%%'suffix'}
|
| 435 |
|
| 436 | The prefix and suffix can be glob patterns, but this usage is discouraged
|
| 437 | because it may be slow.
|
| 438 |
|
| 439 | ### op-patsub
|
| 440 |
|
| 441 | Replace a substring or pattern.
|
| 442 |
|
| 443 | The character after the first `/` can be `/` to replace all occurrences:
|
| 444 |
|
| 445 | $ x=food
|
| 446 |
|
| 447 | $ echo ${x//o/--} # replace 1 o with 2 --
|
| 448 | f----d
|
| 449 |
|
| 450 | It can be `#` or `%` for an anchored replacement:
|
| 451 |
|
| 452 | $ echo ${x/#f/--} # left anchored f
|
| 453 | --ood
|
| 454 |
|
| 455 | $ echo ${x/%d/--} # right anchored d
|
| 456 | foo--
|
| 457 |
|
| 458 | The pattern can also be a glob:
|
| 459 |
|
| 460 | $ echo ${x//[a-z]/o} # replace 1 char with o
|
| 461 | oooo
|
| 462 |
|
| 463 | $ echo ${x//[a-z]+/o} # replace multiple chars
|
| 464 | o
|
| 465 |
|
| 466 | ### op-slice
|
| 467 |
|
| 468 | echo ${a[@]:1:2}
|
| 469 | echo ${@:1:2}
|
| 470 |
|
| 471 | ### op-format
|
| 472 |
|
| 473 | ${x@P} evaluates x as a prompt string, i.e. the string that would be printed if
|
| 474 | PS1=$x.
|
| 475 |
|
| 476 | ---
|
| 477 |
|
| 478 | `${x@Q}` quotes the value of `x`, if necessary, so that it can be evaluated as
|
| 479 | a shell word.
|
| 480 |
|
| 481 | $ x='<'
|
| 482 | $ echo "value = $x, quoted = ${x@Q}."
|
| 483 | value = <, quoted = '<'.
|
| 484 |
|
| 485 | $ x=a
|
| 486 | $ echo "value = $x, quoted = ${x@Q}."
|
| 487 | value = a, quoted = a.
|
| 488 |
|
| 489 | In the second case, the string `a` doesn't need to be quoted.
|
| 490 |
|
| 491 | ---
|
| 492 |
|
| 493 | Format operations like `@Q` generally treat **empty** variables differently
|
| 494 | than **unset** variables.
|
| 495 |
|
| 496 | That is, `${empty@Q}` is the string `''`, while `${unset@Q}` is an empty
|
| 497 | string:
|
| 498 |
|
| 499 | $ x=''
|
| 500 | $ echo "value = $x, quoted = ${x@Q}."
|
| 501 | value = , quoted = ''.
|
| 502 |
|
| 503 | $ unset -v x
|
| 504 | $ echo "value = $x, quoted = ${x@Q}."
|
| 505 | value = , quoted = .
|
| 506 |
|
| 507 | ---
|
| 508 |
|
| 509 | `${x@a}` returns characters that represent the attributes of the `${x}`, or
|
| 510 | more precisely, the *h-value* of `${x}`.
|
| 511 |
|
| 512 | Definitions:
|
| 513 |
|
| 514 | - *h-value* is the variable (or the object that the variable directly points)
|
| 515 | from which the result of `${x}` would originally come.
|
| 516 | - *r-value* is the value of the expansion of `${x}`
|
| 517 |
|
| 518 | For example, with `arr=(1 2 3)`:
|
| 519 |
|
| 520 | <style>
|
| 521 | table {
|
| 522 | width: 100%;
|
| 523 | margin-left: 2em; /* matches p text in manual.css */
|
| 524 | }
|
| 525 | thead {
|
| 526 | text-align: left;
|
| 527 | }
|
| 528 | </style>
|
| 529 |
|
| 530 | <table>
|
| 531 |
|
| 532 | - thead
|
| 533 | - Reference
|
| 534 | - Expression
|
| 535 | - H-value
|
| 536 | - R-value
|
| 537 | - Flags returned
|
| 538 | - tr
|
| 539 | - <!-- empty -->
|
| 540 | - `${arr[0]@a}` or <br/> `${arr@a}`
|
| 541 | - array<br/> `(1 2 3)`
|
| 542 | - string<br/> `1`
|
| 543 | - `a`
|
| 544 | - tr
|
| 545 | - <!-- empty -->
|
| 546 | - `${arr[@]@a}`
|
| 547 | - array<br/> `(1 2 3)`
|
| 548 | - array<br/> `(1 2 3)`
|
| 549 | - `a a a`
|
| 550 | - tr
|
| 551 | - `ref=arr` or `ref=arr[0]`
|
| 552 | - `${!ref@a}`
|
| 553 | - array<br/> `(1 2 3)`
|
| 554 | - string<br/> `1`
|
| 555 | - `a`
|
| 556 | - <!-- empty -->
|
| 557 | - tr
|
| 558 | - `ref=arr[@]`
|
| 559 | - `${!ref@a}`
|
| 560 | - array<br/> `(1 2 3)`
|
| 561 | - array<br/> `(1 2 3)`
|
| 562 | - `a a a`
|
| 563 |
|
| 564 | </table>
|
| 565 |
|
| 566 | When `${x}` would result in a word list, `${x@a}` returns a word list
|
| 567 | containing the attributes of the *h-value* of each word.
|
| 568 |
|
| 569 | ---
|
| 570 |
|
| 571 | These characters may be returned:
|
| 572 |
|
| 573 | <table>
|
| 574 |
|
| 575 | - thead
|
| 576 | - Character
|
| 577 | - Where `${x}` would be obtained
|
| 578 | - tr
|
| 579 | - `a`
|
| 580 | - indexed array
|
| 581 | - tr
|
| 582 | - `A`
|
| 583 | - associative array
|
| 584 | - tr
|
| 585 | - `r`
|
| 586 | - readonly container
|
| 587 | - tr
|
| 588 | - `x`
|
| 589 | - exported variable
|
| 590 | - tr
|
| 591 | - `n`
|
| 592 | - name reference (OSH extension)
|
| 593 |
|
| 594 | </table>
|