github.com/xiaq/elvish@v0.12.0/website/src/ref/builtin.md (about) 1 <!-- toc --> 2 3 # Introduction 4 5 The builtin module contains facilities that are potentially useful to all 6 users. It occupies the `builtin:` namespace. You rarely have to explicitly 7 specify the namespace though, since it is one of the namespaces consulted when 8 resolving unqualified names. 9 10 ## Usage Notation 11 12 The usage of a builtin command is described by giving an example usage, using 13 variables as arguments. For instance, The `repeat` command takes two arguments 14 and are described as: 15 16 ```elvish 17 repeat $n $v 18 ``` 19 20 Optional arguments are represented with a trailing `?`, while variadic 21 arguments with a trailing `...`. For instance, the `count` command takes an optional list: 22 23 ```elvish 24 count $input-list? 25 ``` 26 27 While the `put` command takes an arbitrary number of arguments: 28 29 ```elvish 30 put $values... 31 ``` 32 33 Options are given along with their default values. For instance, the `echo` 34 command takes an `sep` option and arbitrary arguments: 35 36 ```elvish 37 echo &sep=' ' $value... 38 ``` 39 40 (When you calling functions, options are always optional.) 41 42 43 ## Supplying Input 44 45 Some builtin functions, e.g. `count` and `each`, can take their input in one 46 of two ways: 47 48 1. From pipe: 49 50 ```elvish-transcript 51 ~> put lorem ipsum | count # count number of inputs 52 2 53 ~> put 10 100 | each [x]{ + 1 $x } # apply function to each input 54 ▶ 11 55 ▶ 101 56 ``` 57 58 Byte pipes are also possible; one line becomes one input: 59 60 ```elvish-transcript 61 ~> echo "a\nb\nc" | count # count number of lines 62 ▶ 3 63 ``` 64 65 1. From an argument -- an iterable value: 66 67 ```elvish-transcript 68 ~> count [lorem ipsum] # count number of elements in argument 69 2 70 ~> each [x]{ + 1 $x } [10 100] # apply to each element in argument 71 ▶ 11 72 ▶ 101 73 ``` 74 75 Strings, and in future, other sequence types are also possible: 76 77 ```elvish-transcript 78 ~> count lorem 79 ▶ 5 80 ``` 81 82 When documenting such commands, the optional argument is always written as 83 `$input-list?`. On the other hand, a trailing `$input-list?` always indicates 84 that a command can take its input in one of two ways above: this fact is not 85 repeated below. 86 87 **Note**: You should prefer the first form, unless using it requires explicit 88 `put` commands. Avoid `count [(some-command)]` or `each $some-func 89 [(some-command)]`; they are, most of the time, equivalent to `some-command | 90 count` or `some-command | each $some-func`. 91 92 **Rationale**: An alternative way to design this is to make (say) `count` take 93 an arbitrary number of arguments, and count its arguments; when there is 0 94 argument, count inputs. However, this leads to problems in code like `count 95 *`; the intention is clearly to count the number of files in the current 96 directory, but when the current directory is empty, `count` will wait for 97 inputs. Hence it is required to put the input in a list: `count [*]` 98 unambiguously supplies input in the argument, even if there is no file. 99 100 ## Numerical Commands 101 102 Commands that operate on numbers are quite flexible about the format of the 103 numbers. Integers can be specified as decimals (e.g. `233`) or hexadecimals 104 (e.g. `0xE9`) and floating-point numbers can be specified using the scientific 105 notation (e.g. `2.33e2`). These are different strings, but equal when 106 considered as commands. 107 108 Elvish has no special syntax or data type for numbers. Instead, they are just 109 strings. For this reason, builtin commands for strings and numbers are 110 completely separate. For instance, the numerical equality command is `==`, 111 while the string equality command is `==s`. Another example is the `+` 112 builtin, which only operates on numbers and does not function as a string 113 concatenation commands. 114 115 116 ## Predicates 117 118 Predicates are functions that write exactly one output that is either `$true` 119 or `$false`. They are described like "Determine ..." or "Test ...". See 120 [`is`](#is) for one example. 121 122 123 ## "Do Not Use" Functions and Variables 124 125 The name of some variables and functions have a leading `-`. This is a 126 convention to say that it is subject to change and should not be depended 127 upon. They are either only useful for debug purposes, or have known issues in 128 the interface or implementation, and in the worst case will make Elvish crash. 129 (Before 1.0, all features are subject to change, but those ones are sure to be 130 changed.) 131 132 Those functions and variables are documented near the end of the respective 133 sections. Their known problem is also discussed. 134 135 136 # Builtin Functions 137 138 ## + - * / 139 140 ```elvish 141 + $summand... 142 - $minuend $subtrahend... 143 * $factor... 144 / $dividend $divisor... 145 ``` 146 147 Basic arithmetic operations of adding, substraction, multiplication and 148 division respectively. 149 150 All of them can take multiple arguments: 151 152 ```elvish-transcript 153 ~> + 2 5 7 # 2 + 5 + 7 154 ▶ 14 155 ~> - 2 5 7 # 2 - 5 - 7 156 ▶ -10 157 ~> * 2 5 7 # 2 * 5 * 7 158 ▶ 70 159 ~> / 2 5 7 # 2 / 5 / 7 160 ▶ 0.05714285714285715 161 ``` 162 163 When given one element, they all output their sole argument (given that it 164 is a valid number). When given no argument, 165 166 * `+` outputs 0, and `*` outputs 1. You can think that they both have 167 a "hidden" argument of 0 or 1, which does not alter their behaviors (in 168 mathematical terms, 0 and 1 are [identity 169 elements](https://en.wikipedia.org/wiki/Identity_element) of addition and 170 multiplication, respectively). 171 172 * `-` throws an exception. 173 174 * `/` becomes a synonym for `cd /`, due to the implicit cd feature. (The 175 implicit cd feature will probably change to avoid this oddity). 176 177 ## % 178 179 ```elvish 180 % $dividend $divisor 181 ``` 182 183 Output the remainder after dividing `$dividend` by `$divisor`. Both must be 184 integers. Example: 185 186 ```elvish-transcript 187 ~> % 23 7 188 ▶ 2 189 ``` 190 191 ## ^ 192 193 ```elvish 194 ^ $base $exponent 195 ``` 196 197 Output the result of raising `$base` to the power of `$exponent`. Examples: 198 199 ```elvish-transcript 200 ~> ^ 2 10 201 ▶ 1024 202 ~> ^ 2 0.5 203 ▶ 1.4142135623730951 204 ``` 205 206 ## < <= == != > >= 207 208 ```elvish 209 < $number... # less 210 <= $number... # less or equal 211 == $number... # equal 212 != $number... # not equal 213 > $number... # greater 214 >= $number... # greater or equal 215 ``` 216 217 Number comparisons. All of them accept an arbitrary number of arguments: 218 219 1. When given fewer than two arguments, all output `$true`. 220 221 2. When given two arguments, output whether the two arguments satisfy the 222 named relationship. 223 224 3. When given more than two arguments, output whether every adjacent pair of 225 numbers satisfy the named relationship. 226 227 Examples: 228 229 ```elvish-transcript 230 ~> == 3 3.0 231 ▶ $true 232 ~> < 3 4 233 ▶ $true 234 ~> < 3 4 10 235 ▶ $true 236 ~> < 6 9 1 237 ▶ $false 238 ``` 239 240 As a consequence of rule 3, the `!=` command outputs `$true` as long as any 241 *adjacent* pair of numbers are not equal, even if some numbers that are not 242 adjacent are equal: 243 244 ```elvish-transcript 245 ~> != 5 5 4 246 ▶ $false 247 ~> != 5 6 5 248 ▶ $true 249 ``` 250 251 252 ## <s <=s ==s !=s >s >=s 253 254 ```elvish 255 <s $string... # less 256 <=s $string... # less or equal 257 ==s $string... # equal 258 !=s $string... # not equal 259 >s $string... # greater 260 >=s $string... # greater or equal 261 ``` 262 263 String comparisons. They behave similarly to their number counterparts when 264 given multiple arguments. Examples: 265 266 ```elvish-transcript 267 ~> >s lorem ipsum 268 ▶ $true 269 ~> ==s 1 1.0 270 ▶ $false 271 ~> >s 8 12 272 ▶ $true 273 ``` 274 275 276 ## all 277 278 ```elvish 279 all 280 ``` 281 282 Pass inputs, both bytes and values, to the output. 283 284 This is an identity function in pipelines: `a | all | b` is equivalent to `a | 285 b`. It is mainly useful for turning inputs into arguments, like: 286 287 ```elvish-transcript 288 ~> put 'lorem,ipsum' | splits , (all) 289 ▶ lorem 290 ▶ ipsum 291 ``` 292 293 Or capturing all inputs in a variable: 294 295 ```elvish-transcript 296 ~> x = [(all)] 297 foo 298 bar 299 (Press ^D) 300 ~> put $x 301 ▶ [foo bar] 302 ``` 303 304 305 ## assoc 306 307 ```elvish 308 assoc $container $k $v 309 ``` 310 311 Output a slighly modified version of `$container`, such that its value at `$k` 312 is `$v`. Applies to both lists and to maps. 313 314 When `$container` is a list, `$k` may be a negative index. However, slice is 315 not yet supported. 316 317 318 ```elvish-transcript 319 ~> assoc [foo bar quux] 0 lorem 320 ▶ [lorem bar quux] 321 ~> assoc [foo bar quux] -1 ipsum 322 ▶ [foo bar ipsum] 323 ~> assoc [&k=v] k v2 324 ▶ [&k=v2] 325 ~> assoc [&k=v] k2 v2 326 ▶ [&k2=v2 &k=v] 327 ``` 328 329 Etymology: [Clojure](https://clojuredocs.org/clojure.core/assoc). 330 331 $cf dissoc 332 333 334 ## bool 335 336 ```elvish 337 bool $value 338 ``` 339 340 Convert a value to boolean. In Elvish, only `$false` and errors are booleanly 341 false. Everything else, including 0, empty strings and empty lists, is 342 booleanly true: 343 344 ```elvish-transcript 345 ~> bool $true 346 ▶ $true 347 ~> bool $false 348 ▶ $false 349 ~> bool $ok 350 ▶ $true 351 ~> bool ?(fail haha) 352 ▶ $false 353 ~> bool '' 354 ▶ $true 355 ~> bool [] 356 ▶ $true 357 ~> bool abc 358 ▶ $true 359 ``` 360 361 $cf not 362 363 ## cd 364 365 ```elvish 366 cd $dirname 367 ``` 368 369 Change directory. 370 371 Note that Elvish's `cd` does not support `cd -`. 372 373 ## constantly 374 375 ```elvish 376 constantly $value... 377 ``` 378 379 Output a function that takes no arguments and outputs `$value`s when called. 380 Examples: 381 382 ```elvish-transcript 383 ~> f=(constantly lorem ipsum) 384 ~> $f 385 ▶ lorem 386 ▶ ipsum 387 ``` 388 389 The above example is actually equivalent to simply `f = []{ put lorem ipsum }`; 390 it is most useful when the argument is **not** a literal value, e.g. 391 392 ```elvish-transcript 393 ~> f = (constantly (uname)) 394 ~> $f 395 ▶ Darwin 396 ~> $f 397 ▶ Darwin 398 ``` 399 400 The above code only calls `uname` once, while if you do `f = []{ put (uname) }`, 401 every time you invoke `$f`, `uname` will be called. 402 403 Etymology: [Clojure](https://clojuredocs.org/clojure.core/constantly). 404 405 406 ## count 407 408 ```elvish 409 count $input-list? 410 ``` 411 412 Count the number of inputs. 413 414 Examples: 415 416 ```elvish-transcript 417 ~> count lorem # count bytes in a string 418 ▶ 5 419 ~> count [lorem ipsum] 420 ▶ 2 421 ~> range 100 | count 422 ▶ 100 423 ~> seq 100 | count 424 ▶ 100 425 ``` 426 427 428 ## dir-history 429 430 ```elvish 431 dir-history 432 ``` 433 434 Return a list containing the directory history. Each element is a map 435 with two keys: `path` and `score`. The list is sorted by descending 436 score. 437 438 Example: 439 440 ```elvish-transcript 441 ~> dir-history | take 1 442 ▶ [&path=/Users/foo/.elvish &score=96.79928] 443 ``` 444 445 ## dissoc 446 447 ```elvish 448 dissoc $map $k 449 ``` 450 451 Output a slightly modified version of `$map`, with the key `$k` removed. If 452 `$map` does not contain `$k` as a key, the same map is returned. 453 454 ```elvish-transcript 455 ~> dissoc [&foo=bar &lorem=ipsum] foo 456 ▶ [&lorem=ipsum] 457 ~> dissoc [&foo=bar &lorem=ipsum] k 458 ▶ [&lorem=ipsum &foo=bar] 459 ``` 460 461 $cf assoc 462 463 ## drop 464 465 ```elvish 466 drop $n $input-list? 467 ``` 468 469 Drop the first `$n` elements of the input. If `$n` is larger than the number of 470 input elements, the entire input is dropped. 471 472 Example: 473 474 ```elvish-transcript 475 ~> drop 2 [a b c d e] 476 ▶ c 477 ▶ d 478 ▶ e 479 ~> splits ' ' 'how are you?' | drop 1 480 ▶ are 481 ▶ 'you?' 482 ~> range 2 | drop 10 483 ``` 484 485 Etymology: Haskell. 486 487 $cf take 488 489 490 ## each 491 492 ```elvish 493 each $f $input-list? 494 ``` 495 496 Call `$f` on all inputs. Examples: 497 498 ```elvish-transcript 499 ~> range 5 8 | each [x]{ ^ $x 2 } 500 ▶ 25 501 ▶ 36 502 ▶ 49 503 ~> each [x]{ put $x[:3] } [lorem ipsum] 504 ▶ lor 505 ▶ ips 506 ``` 507 508 $cf peach 509 510 Etymology: Various languages, as `for each`. Happens to have the same name as 511 the iteration construct of 512 [Factor](http://docs.factorcode.org/content/word-each,sequences.html). 513 514 515 ## eawk 516 517 ```elvish 518 eawk $f $input-list? 519 ``` 520 521 For each input, call `$f` with the input followed by all its fields. 522 523 It should behave the same as the following functions: 524 525 ```elvish 526 fn eawk [f @rest]{ 527 each [line]{ 528 @fields = (re:split '[ \t]+' 529 (re:replace '^[ \t]+|[ \t]+$' '' $line)) 530 $f $line $@fields 531 } $@rest 532 } 533 ``` 534 535 This command allows you to write code very similar to `awk` scripts using 536 anonymous functions. Example: 537 538 ```elvish-transcript 539 ~> echo ' lorem ipsum 540 1 2' | awk '{ print $1 }' 541 lorem 542 1 543 ~> echo ' lorem ipsum 544 1 2' | eawk [line a b]{ put $a } 545 ▶ lorem 546 ▶ 1 547 ``` 548 549 550 ## echo 551 552 ```elvish 553 echo &sep=' ' $value... 554 ``` 555 556 Print all arguments, joined by the `sep` option, and followed by a newline. 557 558 Examples: 559 560 ```elvish-transcript 561 ~> echo Hello elvish 562 Hello elvish 563 ~> echo "Hello elvish" 564 Hello elvish 565 ~> echo &sep=, lorem ipsum 566 lorem,ipsum 567 ``` 568 569 Notes: The `echo` builtin does not treat `-e` or `-n` specially. For instance, 570 `echo -n` just prints `-n`. Use double-quoted strings to print special 571 characters, and `print` to suppress the trailing newline. 572 573 $cf print 574 575 Etymology: Bourne sh. 576 577 578 ## eq 579 580 ```elvish 581 eq $values... 582 ``` 583 584 Determine whether all `$value`s are structurally equivalent. Writes `$true` 585 when given no or one argument. 586 587 ```elvish-transcript 588 ~> eq a a 589 ▶ $true 590 ~> eq [a] [a] 591 ▶ $true 592 ~> eq [&k=v] [&k=v] 593 ▶ $true 594 ~> eq a [b] 595 ▶ $false 596 ``` 597 598 $cf is not-eq 599 600 Etymology: [Perl](https://perldoc.perl.org/perlop.html#Equality-Operators). 601 602 603 ## exec 604 605 ```elvish 606 exec $command? 607 ``` 608 609 Replace the Elvish process with an external `$command`, defaulting to 610 `elvish`. 611 612 613 ## exit 614 615 ```elvish 616 exit $status? 617 ``` 618 619 Exit the Elvish process with `$status` (defaulting to 0). 620 621 ## explode 622 623 ```elvish 624 explode $iterable 625 ``` 626 627 Put all elements of `$iterable` on the structured stdout. Like `flatten` in 628 functional languages. Equivalent to `[li]{ put $@li }`. 629 630 Example: 631 632 ```elvish-transcript 633 ~> explode [a b [x]] 634 ▶ a 635 ▶ b 636 ▶ [x] 637 ``` 638 639 Etymology: [PHP](http://php.net/manual/en/function.explode.php). PHP's 640 `explode` is actually equivalent to Elvish's `splits`, but the author liked 641 the name too much to not use it. 642 643 644 ## external 645 646 ```elvish 647 external $program 648 ``` 649 650 Construct a callable value for the external program `$program`. Example: 651 652 ```elvish-transcript 653 ~> x = (external man) 654 ~> $x ls # opens the manpage for ls 655 ``` 656 657 $cf has-external search-external 658 659 ## fail 660 661 ```elvish 662 fail $message 663 ``` 664 665 Throw an exception. 666 667 ```elvish-transcript 668 ~> fail bad 669 Exception: bad 670 Traceback: 671 [interactive], line 1: 672 fail bad 673 ~> put ?(fail bad) 674 ▶ ?(fail bad) 675 ``` 676 677 **Note**: Exceptions are now only allowed to carry string messages. You cannot 678 do `fail [&cause=xxx]` (this will, ironically, throw a different exception 679 complaining that you cannot throw a map). This is subject to change. Builtins 680 will likely also throw structured exceptions in future. 681 682 683 ## fclose 684 685 ```elvish 686 fclose $file 687 ``` 688 689 Close a file opened with `fopen`. 690 691 $cf fopen 692 693 694 ## fopen 695 696 ```elvish 697 fopen $filename 698 ``` 699 700 Open a file. Currently, `fopen` only supports opening a file for reading. File 701 must be closed with `fclose` explicitly. Example: 702 703 ```elvish-transcript 704 ~> cat a.txt 705 This is 706 a file. 707 ~> f = (fopen a.txt) 708 ~> cat < $f 709 This is 710 a file. 711 ~> fclose $f 712 ``` 713 714 $cf fclose 715 716 717 ## from-json 718 719 ```elvish 720 from-json 721 ``` 722 723 Takes bytes stdin, parses it as JSON and puts the result on structured stdout. 724 The input can contain multiple JSONs, which can, but do not have to, be 725 separated with whitespaces. 726 727 Examples: 728 729 ```elvish-transcript 730 ~> echo '"a"' | from-json 731 ▶ a 732 ~> echo '["lorem", "ipsum"]' | from-json 733 ▶ [lorem ipsum] 734 ~> echo '{"lorem": "ipsum"}' | from-json 735 ▶ [&lorem=ipsum] 736 ~> # multiple JSONs running together 737 echo '"a""b"["x"]' | from-json 738 ▶ a 739 ▶ b 740 ▶ [x] 741 ~> # multiple JSONs separated by newlines 742 echo '"a" 743 {"k": "v"}' | from-json 744 ▶ a 745 ▶ [&k=v] 746 ``` 747 748 $cf to-json 749 750 751 ## has-external 752 753 ```elvish 754 has-external $command 755 ``` 756 757 Test whether `$command` names a valid external command. Examples (your output 758 might differ): 759 760 ```elvish-transcript 761 ~> has-external cat 762 ▶ $true 763 ~> has-external lalala 764 ▶ $false 765 ``` 766 767 $cf external search-external 768 769 770 ## has-prefix 771 772 ```elvish 773 has-prefix $string $prefix 774 ``` 775 776 Determine whether `$prefix` is a prefix of `$string`. Examples: 777 778 ```elvish-transcript 779 ~> has-prefix lorem,ipsum lor 780 ▶ $true 781 ~> has-prefix lorem,ipsum foo 782 ▶ $false 783 ``` 784 785 ## has-suffix 786 787 ```elvish 788 has-suffix $string $suffix 789 ``` 790 791 Determine whether `$suffix` is a suffix of `$string`. Examples: 792 793 ```elvish-transcript 794 ~> has-suffix a.html .txt 795 ▶ $false 796 ~> has-suffix a.html .html 797 ▶ $true 798 ``` 799 800 801 ## is 802 803 ```elvish 804 is $values... 805 ``` 806 807 Determine whether all `$value`s have the same identity. Writes `$true` when 808 given no or one argument. 809 810 The definition of identity is subject to change. Do not rely on its behavior. 811 812 ```elvish-transcript 813 ~> is a a 814 ▶ $true 815 ~> is a b 816 ▶ $false 817 ~> is [] [] 818 ▶ $true 819 ~> is [a] [a] 820 ▶ $false 821 ``` 822 823 $cf eq 824 825 Etymology: [Python](https://docs.python.org/3/reference/expressions.html#is). 826 827 828 ## joins 829 830 ```elvish 831 joins $sep $input-list? 832 ``` 833 834 Join inputs with `$sep`. Examples: 835 836 ```elvish-transcript 837 ~> put lorem ipsum | joins , 838 ▶ lorem,ipsum 839 ~> joins , [lorem ipsum] 840 ▶ lorem,ipsum 841 ``` 842 843 The suffix "s" means "string" and also serves to avoid colliding with the 844 well-known [join](https://en.wikipedia.org/wiki/join_(Unix)) utility. 845 846 Etymology: Various languages as `join`, in particular 847 [Python](https://docs.python.org/3.6/library/stdtypes.html#str.join). 848 849 $cf splits 850 851 852 ## keys 853 854 ```elvish 855 keys $map 856 ``` 857 858 Put all keys of `$map` on the structured stdout. 859 860 Example: 861 862 ```elvish-transcript 863 ~> keys [&a=foo &b=bar &c=baz] 864 ▶ a 865 ▶ c 866 ▶ b 867 ``` 868 869 Note that there is no guaranteed order for the keys of a map. 870 871 872 ## kind-of 873 874 ```elvish 875 kind-of $value... 876 ``` 877 878 Output the kinds of `$value`s. Example: 879 880 ```elvish-transcript 881 ~> kind-of lorem [] [&] 882 ▶ string 883 ▶ list 884 ▶ map 885 ``` 886 887 The terminology and definition of "kind" is subject to change. 888 889 ## nop 890 891 ```elvish 892 nop &any-opt= $value... 893 ``` 894 895 Accepts arbitrary arguments and options and does exactly nothing. 896 897 Examples: 898 899 ```elvish-transcript 900 ~> nop 901 ~> nop a b c 902 ~> nop &k=v 903 ``` 904 905 Etymology: Various languages, in particular NOP in [assembly 906 languages](https://en.wikipedia.org/wiki/NOP). 907 908 909 ## not 910 911 ```elvish 912 not $value 913 ``` 914 915 Boolean negation. Examples: 916 917 ```elvish-transcript 918 ~> not $true 919 ▶ $false 920 ~> not $false 921 ▶ $true 922 ~> not $ok 923 ▶ $false 924 ~> not ?(fail error) 925 ▶ $true 926 ``` 927 928 **NOTE**: `and` and `or` are implemented as special commands. 929 930 $cf bool 931 932 933 ## not-eq 934 935 ```elvish 936 not-eq $values... 937 ``` 938 939 Determines whether every adjacent pair of `$value`s are not equal. Note that 940 this does not imply that `$value`s are all distinct. Examples: 941 942 ```elvish-transcript 943 ~> not-eq 1 2 3 944 ▶ $true 945 ~> not-eq 1 2 1 946 ▶ $true 947 ~> not-eq 1 1 2 948 ▶ $false 949 ``` 950 951 $cf eq 952 953 954 ## ord 955 956 ```elvish 957 ord $string 958 ``` 959 960 Output value of each codepoint in `$string`, in hexadecimal. Examples: 961 962 ```elvish-transcript 963 ~> ord aA 964 ▶ 0x61 965 ▶ 0x41 966 ~> ord 你好 967 ▶ 0x4f60 968 ▶ 0x597d 969 ``` 970 971 The output format is subject to change. 972 973 Etymology: [Python](https://docs.python.org/3/library/functions.html#ord). 974 975 ## path-* 976 977 ```elvish 978 path-abs $path 979 path-base $path 980 path-clean $path 981 path-dir $path 982 path-ext $path 983 ``` 984 985 See [godoc of path/filepath](https://godoc.org/path/filepath). Go errors are 986 turned into exceptions. 987 988 ## peach 989 990 ```elvish 991 peach $f $input-list? 992 ``` 993 994 Call `$f` on all inputs, possibly in parallel. 995 996 Example (your output will differ): 997 998 ```elvish-transcript 999 ~> range 1 7 | peach [x]{ + $x 10 } 1000 ▶ 12 1001 ▶ 11 1002 ▶ 13 1003 ▶ 16 1004 ▶ 15 1005 ▶ 14 1006 ``` 1007 1008 This command is intended for homogenous processing of possibly unbound data. 1009 If you need to do a fixed number of heterogenous things in parallel, use 1010 `run-parallel`. 1011 1012 $cf each run-parallel 1013 1014 1015 ## pipe 1016 1017 ```elvish 1018 pipe 1019 ``` 1020 1021 Create a new Unix pipe that can be used in redirections. 1022 1023 A pipe contains both the read FD and the write FD. When redirecting command 1024 input to a pipe with `<`, the read FD is used. When redirecting command output 1025 to a pipe with `>`, the write FD is used. It is not supported to redirect both 1026 input and output with `<>` to a pipe. 1027 1028 Pipes have an OS-dependent buffer, so writing to a pipe without an active 1029 reader does not necessarily block. Pipes **must** be explicitly closed with 1030 `prclose` and `pwclose`. 1031 1032 Putting values into pipes will cause those values to be discarded. 1033 1034 Examples (assuming the pipe has a large enough buffer): 1035 1036 ```elvish-transcript 1037 ~> p = (pipe) 1038 ~> echo 'lorem ipsum' > $p 1039 ~> head -n1 < $p 1040 lorem ipsum 1041 ~> put 'lorem ipsum' > $p 1042 ~> head -n1 < $p 1043 # blocks 1044 # $p should be closed with prclose and pwclose afterwards 1045 ``` 1046 1047 $cf prclose pwclose 1048 1049 1050 ## prclose 1051 1052 ```elvish 1053 prclose $pipe 1054 ``` 1055 1056 Close the read end of a pipe. 1057 1058 $cf pwclose pipe 1059 1060 1061 ## put 1062 1063 ```elvish 1064 put $value... 1065 ``` 1066 1067 Takes arbitrary arguments and write them to the structured stdout. 1068 1069 Examples: 1070 1071 ```elvish-transcript 1072 ~> put a 1073 ▶ a 1074 ~> put lorem ipsum [a b] { ls } 1075 ▶ lorem 1076 ▶ ipsum 1077 ▶ [a b] 1078 ▶ <closure 0xc4202607e0> 1079 ``` 1080 1081 Etymology: Various languages, in particular 1082 [C](https://manpages.debian.org/stretch/manpages-dev/puts.3.en.html) and [Ruby](https://ruby-doc.org/core-2.2.2/IO.html#method-i-puts) as `puts`. 1083 1084 1085 ## pprint 1086 1087 ```elvish 1088 pprint $value... 1089 ``` 1090 1091 Pretty-print representations of Elvish values. Examples: 1092 1093 ```elvish-transcript 1094 ~> pprint [foo bar] 1095 [ 1096 foo 1097 bar 1098 ] 1099 ~> pprint [&k1=v1 &k2=v2] 1100 [ 1101 &k2= 1102 v2 1103 &k1= 1104 v1 1105 ] 1106 ``` 1107 1108 The output format is subject to change. 1109 1110 $cf repr 1111 1112 ## print 1113 1114 ```elvish 1115 print &sep=' ' $value... 1116 ``` 1117 1118 Like `echo`, just without the newline. 1119 1120 $cf echo 1121 1122 Etymology: Various languages, in particular 1123 [Perl](https://perldoc.perl.org/functions/print.html) and 1124 [zsh](http://zsh.sourceforge.net/Doc/Release/Shell-Builtin-Commands.html), 1125 whose `print`s do not print a trailing newline. 1126 1127 1128 ## pwclose 1129 1130 ```elvish 1131 pwclose $pipe 1132 ``` 1133 1134 Close the write end of a pipe. 1135 1136 $cf prclose pipe 1137 1138 1139 ## range 1140 1141 ```elvish 1142 range &step=1 $low? $high 1143 ``` 1144 1145 Output `$low`, `$low` + `$step`, ..., proceeding as long as smaller than 1146 `$high`. If not given, `$low` defaults to 0. 1147 1148 Examples: 1149 1150 ```elvish-transcript 1151 ~> range 4 1152 ▶ 0 1153 ▶ 1 1154 ▶ 2 1155 ▶ 3 1156 ~> range 1 6 &step=2 1157 ▶ 1 1158 ▶ 3 1159 ▶ 5 1160 ``` 1161 1162 Beware floating point oddities: 1163 1164 ```elvish-transcript 1165 ~> range 0 0.8 &step=.1 1166 ▶ 0 1167 ▶ 0.1 1168 ▶ 0.2 1169 ▶ 0.30000000000000004 1170 ▶ 0.4 1171 ▶ 0.5 1172 ▶ 0.6 1173 ▶ 0.7 1174 ▶ 0.7999999999999999 1175 ``` 1176 1177 Etymology: 1178 [Python](https://docs.python.org/3/library/functions.html#func-range). 1179 1180 1181 ## rand 1182 1183 ```elvish 1184 rand 1185 ``` 1186 1187 Output a pseudo-random number in the interval [0, 1). Example: 1188 1189 ```elvish-transcript 1190 ~> rand 1191 ▶ 0.17843564133528436 1192 ``` 1193 1194 ## randint 1195 1196 ```elvish 1197 randint $low $high 1198 ``` 1199 1200 Output a pseudo-random integer in the interval [$low, $high). Example: 1201 1202 ```elvish-transcript 1203 ~> # Emulate dice 1204 randint 1 7 1205 ▶ 6 1206 ``` 1207 1208 1209 ## repeat 1210 1211 ```elvish 1212 repeat $n $value 1213 ``` 1214 1215 Output `$value` for `$n` times. Example: 1216 1217 ```elvish-transcript 1218 ~> repeat 0 lorem 1219 ~> repeat 4 NAN 1220 ▶ NAN 1221 ▶ NAN 1222 ▶ NAN 1223 ▶ NAN 1224 ``` 1225 1226 Etymology: [Clojure](https://clojuredocs.org/clojure.core/repeat). 1227 1228 ## replaces 1229 1230 ```elvish 1231 replaces &max=-1 $old $repl $source 1232 ``` 1233 1234 Replace all occurrences of `$old` with `$repl` in `$source`. If `$max` is 1235 non-negative, it determines the max number of substitutions. 1236 1237 **Note**: `replaces` does not support searching by regular 1238 expressions, `$old` is always interpreted as a plain string. Use 1239 [re:replace](re.html#replace) if you need to search by regex. 1240 1241 ## repr 1242 1243 ```elvish 1244 repr $value... 1245 ``` 1246 1247 Writes representation of `$value`s, separated by space and followed by a 1248 newline. Example: 1249 1250 ```elvish-transcript 1251 ~> repr [foo 'lorem ipsum'] "aha\n" 1252 [foo 'lorem ipsum'] "aha\n" 1253 ``` 1254 1255 $cf pprint 1256 1257 Etymology: [Python](https://docs.python.org/3/library/functions.html#repr). 1258 1259 1260 ## resolve 1261 1262 ```elvish 1263 resolve $command 1264 ``` 1265 1266 Resolve `$command`. Command resolution is described in the [language 1267 reference](/ref/language.html). (TODO: actually describe it there.) 1268 1269 Example: 1270 1271 ```elvish-transcript 1272 ~> resolve echo 1273 ▶ <builtin echo> 1274 ~> fn f { } 1275 ~> resolve f 1276 ▶ <closure 0xc4201c24d0> 1277 ~> resolve cat 1278 ▶ <external cat> 1279 ``` 1280 1281 1282 ## run-parallel 1283 1284 ```elvish 1285 run-parallel $callable ... 1286 ``` 1287 1288 Run several callables in parallel, and wait for all of them to finish. 1289 1290 If one or more callables throw exceptions, the other callables continue 1291 running, and a composite exception is thrown when all callables finish 1292 execution. 1293 1294 The behavior of `run-parallel` is consistent with the behavior of pipelines, 1295 except that it does not perform any redirections. 1296 1297 Here is an example that lets you pipe the stdout and stderr of a command to 1298 two different commands: 1299 1300 ```elvish 1301 pout = (pipe) 1302 perr = (pipe) 1303 run-parallel { 1304 foo > $pout 2> $perr 1305 pwclose $pout 1306 pwclose $perr 1307 } { 1308 bar < $pout 1309 prclose $pout 1310 } { 1311 bar2 < $perr 1312 prclose $perr 1313 } 1314 ``` 1315 1316 This command is intended for doing a fixed number of heterogenous things in 1317 parallel. If you need homogenous parallel processing of possibly unbound data, 1318 use `peach` instead. 1319 1320 $cf peach 1321 1322 1323 ## search-external 1324 1325 ```elvish 1326 search-external $command 1327 ``` 1328 1329 Output the full path of the external `$command`. Throws an exception when not 1330 found. Example (your output might vary): 1331 1332 ```elvish-transcript 1333 ~> search-external cat 1334 ▶ /bin/cat 1335 ``` 1336 1337 $cf external has-external 1338 1339 ## slurp 1340 1341 ```elvish 1342 slurp 1343 ``` 1344 1345 Reads bytes input into a single string, and put this string on structured 1346 stdout. 1347 1348 Example: 1349 1350 ```elvish-transcript 1351 ~> echo "a\nb" | slurp 1352 ▶ "a\nb\n" 1353 ``` 1354 1355 Etymology: Perl, as 1356 [`File::Slurp`](http://search.cpan.org/~uri/File-Slurp-9999.19/lib/File/Slurp.pm). 1357 1358 1359 ## splits 1360 1361 ```elvish 1362 splits $sep $string 1363 ``` 1364 1365 Split `$string` by `$sep`. If `$sep` is an empty string, split it into 1366 codepoints. 1367 1368 ```elvish-transcript 1369 ~> splits , lorem,ipsum 1370 ▶ lorem 1371 ▶ ipsum 1372 ~> splits '' 你好 1373 ▶ 你 1374 ▶ 好 1375 ``` 1376 1377 **Note**: `splits` does not support splitting by regular expressions, 1378 `$sep` is always interpreted as a plain string. Use 1379 [re:split](re.html#split) if you need to split by regex. 1380 1381 Etymology: Various languages as `split`, in particular 1382 [Python](https://docs.python.org/3.6/library/stdtypes.html#str.split). 1383 1384 $cf joins 1385 1386 1387 ## src 1388 1389 ```elvish 1390 src 1391 ``` 1392 1393 Output a map-like value describing the current source being evaluated. The 1394 value contains the following fields: 1395 1396 * `type`, which can be one of `interactive`, `script` or `module`; 1397 1398 * `name`, which is set to the name under which a script is executed or a 1399 module is imported. It is an empty string when `type` = `interactive`; 1400 1401 * `path`, which is the path to the current source. It is an empty string 1402 when `type` = `interactive`; 1403 1404 * `code`, which is the full body of the current source. 1405 1406 Examples: 1407 1408 ```elvish-transcript 1409 ~> put (src)[type name path code] 1410 ▶ interactive 1411 ▶ '' 1412 ▶ '' 1413 ▶ 'put (src)[type name path code]' 1414 ~> echo 'put (src)[type name path code]' > foo.elv 1415 ~> elvish foo.elv 1416 ▶ script 1417 ▶ foo.elv 1418 ▶ /home/xiaq/foo.elv 1419 ▶ "put (src)[type name path code]\n" 1420 ~> echo 'put (src)[type name path code]' > ~/.elvish/lib/m.elv 1421 ~> use m 1422 ▶ module 1423 ▶ m 1424 ▶ /home/xiaq/.elvish/lib/m.elv 1425 ▶ "put (src)[type name path code]\n" 1426 ``` 1427 1428 Note: this builtin always returns information of the source of the **calling 1429 function**. Example: 1430 1431 ```elvish-transcript 1432 ~> echo 'fn f { put (src)[type name path code] }' > ~/.elvish/lib/n.elv 1433 ~> use n 1434 ~> n:f 1435 ▶ module 1436 ▶ n 1437 ▶ /home/xiaq/.elvish/lib/n.elv 1438 ▶ "fn f { put (src)[type name path code] }\n" 1439 ``` 1440 1441 1442 ## styled 1443 1444 ```elvish 1445 styled $object $style-transformer... 1446 ``` 1447 1448 Construct a styled text by applying the supplied transformers to the supplied 1449 object. `$object` can be either a string, a styled segment (see below), a styled 1450 text or an arbitrary concatenation of them. A `$style-transformer` is either: 1451 1452 * The name of a builtin style transformer 1453 * On of the attribute names `bold`, `dim`, `italic`, `underlined`, `blink` or 1454 `inverse` for setting the corresponding attribute 1455 * An attribute name prefixed by `no-` for unsetting the attribute 1456 * An attribute name prefixed by `toggle-` for toggling the attribute between set 1457 and unset 1458 * One of the color names `black`, `red`, `green`, `yellow`, `blue`, `magenta`, 1459 `cyan`, `lightgray`, `gray`, `lightred`, `lightgreen`, `lightyellow`, 1460 `lightblue`, `lightmagenta`, `lightcyan` or `white` for setting the text color 1461 * A color name prefixed by `bg-` to set the background color 1462 * A lambda that receives a styled segment as the only argument and returns a 1463 single styled segment 1464 * A function with the same properties as the lambda (provided via the 1465 `$transformer~` syntax) 1466 1467 When a styled text is converted to a string the corresponding [ANSI SGR code](https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_.28Select_Graphic_Rendition.29_parameters) 1468 is built to render the style. 1469 1470 A styled text is nothing more than a wrapper around a list of styled segments. They 1471 can be accessed by indexing into it. 1472 1473 ```elvish 1474 s = (styled abc red)(styled def green) 1475 put $s[0] $s[1] 1476 ``` 1477 1478 1479 ## styled-segment 1480 1481 ```elvish 1482 styled-segment $object &fg-color=default &bg-color=default &bold=$false &dim=$false &italic=$false &underlined=$false &blink=$false &inverse=$false 1483 ``` 1484 1485 Constructs a styled segment and is a helper function for styled transformers. 1486 `$object` can be a plain string, a styled segment or a concatenation thereof. 1487 Probably the only reason to use it is to build custom style transformers: 1488 1489 ```elvish 1490 fn my-awesome-style-transformer [seg]{ styled-segment $seg &bold=(not $seg[dim]) &dim=(not $seg[italic]) &italic=$seg[bold] } 1491 styled abc $my-awesome-style-transformer~ 1492 ``` 1493 1494 As just seen the properties of styled segments can be inspected by indexing into it. 1495 Valid indices are the same as the options to `styled-segment` plus `text`. 1496 1497 ```elvish 1498 s = (styled-segment abc &bold) 1499 put $s[text] 1500 put $s[fg-color] 1501 put $s[bold] 1502 ``` 1503 1504 1505 ## take 1506 1507 ```elvish 1508 take $n $input-list? 1509 ``` 1510 1511 Retain the first `$n` input elements. If `$n` is larger than the number of 1512 input elements, the entire input is retained. Examples: 1513 1514 ```elvish-transcript 1515 ~> take 3 [a b c d e] 1516 ▶ a 1517 ▶ b 1518 ▶ c 1519 ~> splits ' ' 'how are you?' | take 1 1520 ▶ how 1521 ~> range 2 | take 10 1522 ▶ 0 1523 ▶ 1 1524 ``` 1525 1526 1527 Etymology: Haskell. 1528 1529 1530 ## tilde-abbr 1531 1532 ```elvish 1533 tilde-abbr $path 1534 ``` 1535 1536 If `$path` represents a path under the home directory, replace the 1537 home directory with `~`. Examples: 1538 1539 ```elvish-transcript 1540 ~> echo $E:HOME 1541 /Users/foo 1542 ~> tilde-abbr /Users/foo 1543 ▶ '~' 1544 ~> tilde-abbr /Users/foobar 1545 ▶ /Users/foobar 1546 ~> tilde-abbr /Users/foo/a/b 1547 ▶ '~/a/b' 1548 ``` 1549 1550 1551 ## to-json 1552 1553 ```elvish 1554 to-json 1555 ``` 1556 1557 Takes structured stdin, convert it to JSON and puts the result on bytes 1558 stdout. 1559 1560 ```elvish-transcript 1561 ~> put a | to-json 1562 "a" 1563 ~> put [lorem ipsum] | to-json 1564 ["lorem","ipsum"] 1565 ~> put [&lorem=ipsum] | to-json 1566 {"lorem":"ipsum"} 1567 ``` 1568 1569 $cf from-json 1570 1571 ## to-string 1572 1573 ```elvish 1574 to-string $value... 1575 ``` 1576 1577 Convert arguments to string values. 1578 1579 ```elvish-transcript 1580 ~> to-string foo [a] [&k=v] 1581 ▶ foo 1582 ▶ '[a]' 1583 ▶ '[&k=v]' 1584 ``` 1585 1586 ## wcswidth 1587 1588 ```elvish 1589 wcswidth $string 1590 ``` 1591 1592 Output the width of `$string` when displayed on the terminal. Examples: 1593 1594 ```elvish-transcript 1595 ~> wcswidth a 1596 ▶ 1 1597 ~> wcswidth lorem 1598 ▶ 5 1599 ~> wcswidth 你好,世界 1600 ▶ 10 1601 ``` 1602 1603 1604 ## -gc 1605 1606 ```elvish 1607 -gc 1608 ``` 1609 1610 Force the Go garbage collector to run. 1611 1612 This is only useful for debug purposes. 1613 1614 1615 ## -ifaddrs 1616 1617 ```elvish 1618 -ifaddrs 1619 ``` 1620 1621 Output all IP addresses of the current host. 1622 1623 This should be part of a networking module instead of the builtin module. 1624 1625 ## -log 1626 1627 ```elvish 1628 -log $filename 1629 ``` 1630 1631 Direct internal debug logs to the named file. 1632 1633 This is only useful for debug purposes. 1634 1635 1636 ## -stack 1637 1638 ```elvish 1639 -stack 1640 ``` 1641 1642 1643 Print a stack trace. 1644 1645 This is only useful for debug purposes. 1646 1647 1648 ## -source 1649 1650 ```elvish 1651 -source $filename 1652 ``` 1653 1654 Read the named file, and evaluate it in the current scope. 1655 1656 Examples: 1657 1658 ```elvish-transcript 1659 ~> cat x.elv 1660 echo 'executing x.elv' 1661 foo = bar 1662 ~> -source x.elv 1663 executing x.elv 1664 ~> echo $foo 1665 bar 1666 ``` 1667 1668 Note that while in the example, you can reference `$foo` after sourcing 1669 `x.elv`, putting the `-source` command and reference to `$foo` in the **same 1670 code chunk** (e.g. by using <span class="key">Alt-Enter</span> to insert a 1671 literal Enter, or using `;`) is invalid: 1672 1673 ```elvish-transcript 1674 ~> # A new Elvish session 1675 ~> cat x.elv 1676 echo 'executing x.elv' 1677 foo = bar 1678 ~> -source x.elv; echo $foo 1679 Compilation error: variable $foo not found 1680 [interactive], line 1: 1681 -source x.elv; echo $foo 1682 ``` 1683 1684 This is because the reading of the file is done in the evaluation phase, while 1685 the check for variables happens at the compilation phase (before evaluation). 1686 So the compiler has no evidence showing that `$foo` is actually valid, and 1687 will complain. (See [here](/learn/unique-semantics.html#execution-phases) for 1688 a more detailed description of execution phases.) 1689 1690 To work around this, you can add a forward declaration for `$foo`: 1691 1692 ```elvish-transcript 1693 ~> # Another new session 1694 ~> cat x.elv 1695 echo 'executing x.elv' 1696 foo = bar 1697 ~> foo = ''; -source x.elv; echo $foo 1698 executing x.elv 1699 bar 1700 ``` 1701 1702 1703 ## -time 1704 1705 ```elvish 1706 -time $callable 1707 ``` 1708 1709 Run the callable, and write the time used to run it. Example: 1710 1711 ```elvish-transcript 1712 ~> -time { sleep 1 } 1713 1.006060647s 1714 ``` 1715 1716 When the callable also produces outputs, they are a bit tricky to separate 1717 from the output of `-time`. The easiest workaround is to redirect the output 1718 into a temporary file: 1719 1720 ```elvish-transcript 1721 ~> f = (mktemp) 1722 ~> -time { { echo output; sleep 1 } > $f } 1723 1.005589823s 1724 ~> cat $f 1725 output 1726 ~> rm $f 1727 ``` 1728 1729 1730 # Builtin Variables 1731 1732 ## $_ 1733 1734 A blackhole variable. 1735 1736 Values assigned to it will be discarded. Trying to use its value (like `put 1737 $_`) causes an exception. 1738 1739 ## $after-chdir 1740 1741 A list of functions to run after changing directory. These functions are 1742 always called with directory to change it, which might be a relative path. The 1743 following example also shows `$before-chdir`: 1744 1745 ```elvish-transcript 1746 ~> before-chdir = [[dir]{ echo "Going to change to "$dir", pwd is "$pwd }] 1747 ~> after-chdir = [[dir]{ echo "Changed to "$dir", pwd is "$pwd }] 1748 ~> cd /usr 1749 Going to change to /usr, pwd is /Users/xiaq 1750 Changed to /usr, pwd is /usr 1751 /usr> cd local 1752 Going to change to local, pwd is /usr 1753 Changed to local, pwd is /usr/local 1754 /usr/local> 1755 ``` 1756 1757 $cf before-chdir 1758 1759 ## $args 1760 1761 A list containing command-line arguments. Analogous to `argv` in some other 1762 languages. Examples: 1763 1764 ```elvish-transcript 1765 ~> echo 'put $args' > args.elv 1766 ~> elvish args.elv foo -bar 1767 ▶ [foo -bar] 1768 ~> elvish -c 'put $args' foo -bar 1769 ▶ [foo -bar] 1770 ``` 1771 1772 As demonstrated above, this variable does not contain the name of the script 1773 used to invoke it. For that information, use the `src` command. 1774 1775 $cf src 1776 1777 ## $before-chdir 1778 1779 A list of functions to run before changing directory. These functions are 1780 always called with the new working directory. 1781 1782 $cf after-chdir 1783 1784 ## $false 1785 1786 The boolean false value. 1787 1788 ## $ok 1789 1790 The special value used by `?()` to signal absence of exceptions. 1791 1792 ## $value-out-indicator 1793 1794 A string put before value outputs (such as those of of `put`). Defaults to 1795 `'▶ '`. Example: 1796 1797 ```elvish-transcript 1798 ~> put lorem ipsum 1799 ▶ lorem 1800 ▶ ipsum 1801 ~> value-out-indicator = 'val> ' 1802 ~> put lorem ipsum 1803 val> lorem 1804 val> ipsum 1805 ``` 1806 1807 Note that you almost always want some trailing whitespace for readability. 1808 1809 ## $paths 1810 1811 A list of search paths, kept in sync with `$E:PATH`. It is easier to use than 1812 `$E:PATH`. 1813 1814 ## $pid 1815 1816 The process ID of the current Elvish process. 1817 1818 ## $pwd 1819 1820 The present working directory. Setting this variable has the same effect as 1821 `cd`. This variable is most useful in temporary assignment. 1822 1823 Example: 1824 1825 ```elvish 1826 ## Updates all git repositories 1827 for x [*/] { 1828 pwd=$x { 1829 if ?(test -d .git) { 1830 git pull 1831 } 1832 } 1833 } 1834 ``` 1835 1836 Etymology: the `pwd` command. 1837 1838 ## $true 1839 1840 The boolean true value.