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 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'\']} ?
The setvar keyword feels too long.

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: command subs may be optimized later, as ksh does.)

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 at some point. However, that must be done carefully, as a proc composes with processes, but a func doesn't.

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'\']} ?

Oil has two array index syntax:

No:

echo ${array[r'\']}

Yes:

echo $[array[r'\']]

A similar issue exists with arithmetic.

Old:

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

New:

echo $[1 + 2]     # Oil expression

The setvar keyword feels too long.

Yes, but if you upgrade to bin/oil instead of bin/osh, you can almost always use set (which is an alias for setlocal, not setvar).

OSH style, works everywhere:

setvar x = 42

Oil style, which OK for new scripts, may need to change old scripts:

set x = 42  # nicer, but conflicts with set -o errexit

To avoid the set conflict, change this:

set -o errexit
set +o errexit

to one of these:

shopt --set errexit
shopt --unset errexit

# same thing
shopt -s errexit
shopt -u errexit

Generated on Sat Jan 23 00:06:31 PST 2021