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) ?
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.)

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 Thu Oct 8 13:33:44 PDT 2020