YSH vs. Shell

This doc may help shell users understand YSH. If you don't want to read a comparison, see A Tour of YSH.

Table of Contents
YSH is Stricter at Parse Time, and Runtime
Three Core Sublanguages, Instead of 4
Python-like Expressions Replace Arith and Bool
Command Sublanguage
Word Sublanguage
Runtime
Builtin Commands and Functions
Shell Options, shvar, Registers
Data Languages, Not Ad Hoc Parsing
Shell Features Retained
Related Links

YSH is Stricter at Parse Time, and Runtime

OSH and YSH both prefer static parsing, so you get syntax errors up front. In shell, syntax errors can occur at runtime.

At runtime, we have strict_* shell options that handle edge cases. YSH generally fails faster than shell. They're in the option group strict:all.

Three Core Sublanguages, Instead of 4

See the List of Sublanguages on the blog.

Python-like Expressions Replace Arith and Bool

This means that all these constructs are discouraged in favor of YSH expressions:

[[ $x =~ $pat ]]

x=$(( x + 1 ))
(( x = x + 1 ))
let x=x+1

declare -A assoc=(['k1']=v1 ['k2']=v2)

See YSH vs. Shell Idioms for more rewrites.

Command Sublanguage

Notable differences:

Curly Braces { }, instead of then fi and do done.

Keywords for Variable Assignment like var, const, setvar, instead of builtins like local, readonly, myvar=foo, etc.

Array literals like var a = :| ale bean | instead of local a=(ale bean)

Procs, Funcs, and Blocks for modularity:

Multiline strings replace here docs.

fork and forkwait builtins, instead of & and ()

Parentheses are instead used for Python-like expressions, e.g.

if (x > 0) {
  echo 'positive'
}

Word Sublanguage

Notable differences:

Simple Word Evaluation replaces implicit word splitting, and dynamic parsing/evaluation of globs. It adds splicing of Lists into argv arrays.

Expression substitution like echo $[42 + a[i]].

This includes function calls: echo $[join(['pea', nut'])]

Raw strings can have an r prefix, like echo r'C:\Program Files\'.

Runtime

Builtin Commands and Functions

Shell Options, shvar, Registers

We upgrade bash's shopt mechanism with more options, like shopt --set parse_brace. These global options are controlled with scope

shopt --unset errexit {
  rm /tmp/*
  rm /etc/*
}

A shvar is similar to a shopt, but it has a string value, like $IFS and $PATH.

shvar PATH=. {
  my-command /tmp
}

Registers are special variables set by the interpreter, beginning with _:

Data Languages, Not Ad Hoc Parsing

YSH programs are encouraged to use our JSON-like data languages to serialize data.

For example, using an encoded array like ["one\n", "two \t three"] results in more obviously correct code than using ad hoc delimiters like spaces, commas, or colons.

Shell Features Retained

These bash features are still idiomatic in YSH:

Related Links

Generated on Mon, 28 Oct 2024 01:32:45 +0000