source | all docs for version 0.8.0 | all versions | oilshell.org
Warning: Work in progress! Leave feedback on Zulip or Github if you'd like this doc to be updated.
const
, var
, and setvar
are shell-like and interactiveShell:
readonly c=C
myfunc() {
local x=L
x=mutated
newglobal=G
}
OSH:
const c = 'C'
proc myproc {
var x = 'L'
setvar x = 'mutated'
setvar newglobal = 'G'
}
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
.var
and set
/setglobal
are Oil-like and stricterset
mutates a local that's been declaredsetglobal
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
}
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
setref
is for "out parameters"To return a value. Like "named references" in bash.
proc decode (s, :out) {
setref out = '123'
}
=
pretty prints an expressionUseful interactively.
$ = 'foo'
(Str) 'foo'
$ = %(one two)
(StrArray) ['one', 'two']
proc
declares a shell-like "function"proc p {
echo one
echo two
}
p > file.txt
return
is a keyword in OilIt takes an expression, not a word. See command vs. expression mode.
proc p {
var status = '1'
echo 'hello'
return status # not $status
}
p
echo $? # prints 1
(This is intended to be consistent with a future func
.)