| 1 | ---
 | 
| 2 | default_highlighter: oils-sh
 | 
| 3 | ---
 | 
| 4 | 
 | 
| 5 | YSH Language Warts
 | 
| 6 | ==================
 | 
| 7 | 
 | 
| 8 | This documents describes parts of the YSH language that may be surprising.
 | 
| 9 | 
 | 
| 10 | All languages have warts, but most of them don't document them for you!  Even a
 | 
| 11 | nice language like Python has surprising cases like `42,` and `f(x),` being a
 | 
| 12 | 1-tuple (because of the trailing comma).
 | 
| 13 | 
 | 
| 14 | The goal of YSH is to remove the many warts of shell, documented at [Shell
 | 
| 15 | WTFs][wtfs].  Nonetheless it has some of its own.
 | 
| 16 | 
 | 
| 17 | [wtfs]: https://github.com/oilshell/oil/wiki/Shell-WTFs
 | 
| 18 | 
 | 
| 19 | <div id="toc">
 | 
| 20 | </div>
 | 
| 21 | 
 | 
| 22 | ## For Bash Compatibility
 | 
| 23 | 
 | 
| 24 | ### Two Left Parens Should be Separated By Space
 | 
| 25 | 
 | 
| 26 | No:
 | 
| 27 | 
 | 
| 28 |     if ((x + 1) < n) {  # note ((
 | 
| 29 |       echo 'less'
 | 
| 30 |     }
 | 
| 31 | 
 | 
| 32 | Yes:
 | 
| 33 | 
 | 
| 34 |     if ( (x + 1) < n) {  # add a space
 | 
| 35 |       echo 'less'
 | 
| 36 |     }
 | 
| 37 | 
 | 
| 38 | This is because the `((` token is for bash arithmetic, which is disallowed in
 | 
| 39 | YSH.
 | 
| 40 | 
 | 
| 41 | ### Two Different Syntaxes For `Block` and `Expr` Literals
 | 
| 42 | 
 | 
| 43 | Blocks look different in command vs expression mode:
 | 
| 44 | 
 | 
| 45 |     cd /tmp {                   # command mode { }
 | 
| 46 |       echo $PWD
 | 
| 47 |     }
 | 
| 48 |     var myblock = ^(echo $PWD)  # expression mode, lazy ^( )
 | 
| 49 | 
 | 
| 50 | So do expressions:
 | 
| 51 | 
 | 
| 52 |     myproc | where (age > 10)   # command mode, lazy ( )
 | 
| 53 |     var myval = age > 10        # expression mode
 | 
| 54 |     var myexpr = ^[age > 10]    # expression mode, lazy ^[ ]
 | 
| 55 | 
 | 
| 56 | It would have been nicer if they were consistent, but shell is already
 | 
| 57 | inconsistent with `$(echo hi)` and `{ echo hi; }`.
 | 
| 58 | 
 | 
| 59 | There is consistency in other directions:
 | 
| 60 | 
 | 
| 61 | - `^(echo $PWD)` is consistent with shell's eagerly evaluated `$(echo $PWD)`.
 | 
| 62 | - `^[42 + f(x)]` is consistent with expression sub `$[42 + f(x)]`.
 | 
| 63 | 
 | 
| 64 | Most users won't see these literal forms very much.  They're more useful for
 | 
| 65 | testing and frameworks rather than simple scripts/applications.
 | 
| 66 | 
 | 
| 67 | ## Related 
 | 
| 68 | 
 | 
| 69 | - The doc on [compatibility quirks](quirks.html) relates to the OSH language.
 | 
| 70 | 
 |