Why Sponsor Oils? | source | all docs for version 0.18.0 | all versions | oilshell.org
Warning: Work in progress! Leave feedback on Zulip or Github if you'd like this doc to be updated.
This chapter in the Oils Reference describes the YSH expression language, which includes Egg Expressions.
Initializes a constant name to the Oil expression on the right.
const c = 'mystr' # equivalent to readonly c=mystr
const pat = / digit+ / # an eggex, with no shell equivalent
It's either a global constant or scoped to the current function.
Initializes a name to the Oil expression on the right.
var s = 'mystr' # equivalent to declare s=mystr
var pat = / digit+ / # an eggex, with no shell equivalent
It's either global or scoped to the current function.
At the top-level, setvar creates or mutates a variable.
Inside a proc, it mutates a local variable declared with var.
Creates or mutates a global variable.
Mutates a variable through a named reference. See examples in doc/variables.md.
Oil uses JavaScript-like spellings for these three "atoms":
true false null
Note that the empty string is a good "special" value in some cases. The null
value can't be interpolated into words.
var myint = 42
var myfloat = 3.14
var float2 = 1e100
#'a' #'_' \n \\ \u{3bc}
Oil strings appear in expression contexts, and look like shell strings:
var s = 'foo'
var double = "hello $world and $(hostname)"
However, strings with backslashes are forced to specify whether they're raw strings or C-style strings:
var s = 'line\n' # parse error: ambiguous
var s = $'line\n' # C-style string
var s = r'[a-z]\n' # raw strings are useful for regexes (not eggexes)
var unicode = 'mu = \u{3bc}'
Lists have a Python-like syntax:
var mylist = ['one', 'two', 3]
And a shell-like syntax:
var list2 = %| one two |
The shell-like syntax accepts the same syntax that a command can:
ls $mystr @ARGV *.py {foo,bar}@example.com
# Rather than executing ls, evaluate and store words
var cmd = :| ls $mystr @ARGV *.py {foo,bar}@example.com |
{name: 'value'}
var myblock = ^(echo $PWD)
var myexpr = ^[1 + 2*3]
var s = 's'
var concat1 = s ++ '_suffix'
var concat2 = "${s}_suffix" # similar
var c = :| one two |
var concat3 = c ++ :| three 4 |
var concat4 = :| @c three 4 |
var mydict = {a: 1, b: 2}
var otherdict = {a: 10, c: 20}
var concat5 = mydict ++ otherdict
a == b # Python-like equality, no type conversion
3 ~== 3.0 # True, type conversion
3 ~== '3' # True, type conversion
3 ~== '3.0' # True, type conversion
not and or
Note that these are distinct from ! && ||
.
+ - * / // % **
~ & | ^
Like Python:
display = 'yes' if len(s) else 'empty'
Like Python:
myarray[3]
mystr[3]
TODO: Does string indexing give you an integer back?
Like Python:
myarray[1 : -1]
mystr[1 : -1]
Like Python:
f(x, y)
~ !~ ~~ !~~
Not implemented.