Oil Language FAQ

Here are some common questions about the Oil language. Many of the answers boil down to the fact that Oil is a smooth upgrade from bash.

Old and new constructs exist side-by-side. New constructs have fewer "gotchas".

Table of Contents
What's the difference myvar, $myvar, and "$myvar" ?
What's the difference between $(dirname $x) and $len(x) ?
How can I return rich values from shell functions / Oil procs?
Why doesn't a raw string work here: ${array[r'\']} ?
Related

What's the difference myvar, $myvar, and "$myvar" ?

Oil is more like Python/JavaScript rather than PHP/Perl, so it doesn't use the $ sigil as much.

Never use $ on the left-hand side:

var mystr = "foo"   # not var $mystr

Use $ to substitute vars into commands:

echo $mystr
echo $mystr/subdir  # no quotes in commands

or quoted strings:

echo "$mystr/subdir"
var x = "$mystr/subdir"

Rarely use $ on the right-hand side:

var x = mystr       # preferred
var x = $mystr      # ILLEGAL -- use remove $
var x = ${mystr:-}  # occasionally useful

var x = $?          # allowed

See Command vs. Expression Mode for more details.

What's the difference between $(dirname $x) and $len(x) ?

Superficially, both of these syntaxes take an argument x and return a string. But they are different:

(Note: builtin subs like ${.myproc $x} are meant to eliminate process overhead, but they're not yet implemented.)

How can I return rich values from shell functions / Oil procs?

There are two primary ways:

(Oil may grow true functions with the func keyword, but it will be built on top of proc and the builtin sub mechanism.)

Send us feedback if this doesn't make sense, or if you want a longer explanation.

Why doesn't a raw string work here: ${array[r'\']} ?

This boils down to the difference between OSH and Oil, and not being able to mix the two. Though they look similar, ${array[i]} syntax (with braces) is fundamentally different than $[array[i]] syntax (with brackets).

Of course, Oil style is preferred when compatibility isn't an issue.

No:

echo ${array[r'\']}

Yes:

echo $[array[r'\']]

A similar issue exists with arithmetic.

Old:

echo $((1 + 2))   # shell arithmetic

New:

echo $[1 + 2]     # Oil expression

Related


Generated on Mon Feb 13 22:06:50 EST 2023