source | all docs for version 0.8.6 | all versions | oilshell.org
Warning: Work in progress! Leave feedback on Zulip or Github if you'd like this doc to be updated.
Related:
readonly
, local
, name=val
They don't allow expressions on the right.
const
, var
, and setvar
const
declares a constant, like readonly
var
declares a local or globalsetvar
mutates a local, mutates a global, or creates a new global.
Shell:
g=G # global variable
readonly c=C # global constant
myfunc() {
local x=X # local variable
readonly y=Y # local constant
x=mutated # mutate local
g=mutated # mutate global
newglobal=G # create new global
caller_var=mutated # dynamic scope (Oil doesn't have this)
}
Oil:
var g = 'G' # global variable
const c = 'C' # global constant
proc myproc {
var x = 'L' # local variable
const y = 'Y' # local constant
setvar x = 'mutated' # mutate local
setvar g = 'mutated' # mutate global
setvar newglobal = 'G' # create new global
# For dynamic scope, Oil uses setref and an
# explicit ref param. See below.
}
var
declares a new variable in the current scope (global or local)const
is like var
, except the binding can never be changedsetvar x = 'y'
is like x=y
in shell (except that it doesn't obey dynamic
scope.)
x
exists, it mutates it.x
.set
rather than setvar
.const
(optional), var
, set
/setglobal
set
mutates a local that's been declared (also setlocal
)setglobal
mutates a global that's been decalredc = 'X'
is syntactic sugar for const c = 'X'
. This is to make it more
compact, i.e. for "Huffman coding" of programs.c = 'X' # syntactic sugar for const c = 'X'
proc myproc {
var x = 'L'
set x = 'mutated'
set notglobal = 'G' # ERROR: neither a local or global
}
It's rarely necessary to mutate globals in shell scripts, but if you do, use
the setglobal
keyword:
var g = 'G'
proc myproc {
setglobal g = 'mutated'
setglobal notglobal = 'G' # ERROR: not a global
}
Just like with assignments.
=
Pretty Prints an ExpressionUseful interactively.
$ = 'foo'
(Str) 'foo'
$ = %(one two)
(StrArray) ['one', 'two']
_
Ignores an ExpressionThink of this:
_ f(x)
as a shortcut for:
_ = f(x) # assign to "meh" variable
setref
for "Out Params" (advanced)To return a value. Like "named references" in bash.
proc decode (s, :out) {
setref out = '123'
}
auto
for Autovivification (future, not implemented)auto count += 1
auto hist['key'] += 1
auto total += 3.5
auto hist['key'] += 4.6
You can use setvar
or set
:
Shell:
a=(one two three)
a[0]=zz
Oil:
var a = %(one two three)
set a[0] = 'zz'
setvar a[0] = 'zz' # also acceptable
Shell:
declare -A A=(['name']=foo ['type']='dir')
A['type']=file
Oil:
var A = {name: 'foo', type: 'dir'}
set A['type'] = 'file'
setvar A['type'] = 'file' # also acceptable
proc
Disables Dynamic ScopeRecall that procs are the way to declare shell-like functions in Oil.
proc p {
echo one
echo two
}
p > file.txt
They mostly look like and work like shell functions, but they change scoping rules.
local
-> var
readonly
-> const
x=mystr
-> setvar x = 'mystr'
x=(my array)
-> setvar x = %(my array)
Advantage: you get expressions on the right.