Warning: Work in progress! Leave feedback on Zulip or Github if you'd like this doc to be updated.

Oil Options

Table of Contents
The Most Important Thing, Quickly Explained
Philosophy For Option Groups
Naming Conventions
Details
That Affect Parsing
That Affect Runtime Behavior
Strict Option Produce More Errors

The Most Important Thing, Quickly Explained

Put one of these lines at the top of your script.

If you still want to run your script with other shells:

shopt -s strict:all 2>/dev/null || true

If you want to run with some Oil enhancements:

shopt --set oil:basic

This is unlikely to break existing scripts, but it's possible. See Shell Language Deprecations.

Or use bin/oil for a brand new Oil script, opting into all enhancements. Your shebang line might be #!/usr/bin/env oil. This is the equivalent of shopt --set oil:all when running bin/osh.

That's all most users need to know. These option groups allow you to gradually opt into Oil.

Philosophy For Option Groups

TODO: Do we need simple:all?

Naming Conventions

Details

TODO: Polish everything below.


This is how you opt into the Oil language:

shopt --set oil:all

It turns on:

When you care about running your script under other shells, use shopt -s strict:all, which is documented in the OSH manual.

That Affect Parsing

Options that affect parsing start with parse-.

echo @words

and inline function calls.

echo @split(x)

See examples below.


shopt -s parse_brace does three things:

Test cases start here:

https://github.com/oilshell/oil/blob/master/spec/oil-options.test.sh#L257

Examples:

if test -d / {
  echo one
} elif test -d /tmp {
  echo two
} else {
   echo none
}
# can also be put all on one line

while true {
  echo hi
  break
}

for x in a b c {
  echo $x
}

case $x {
  *.py)
    echo python
    ;;
  *.sh)
    echo shell
    ;;
}

What's the motivation for this? Mainly familiarity: I hear a lot of feedback that nobody can remember how to write an if statement or a for loop in shell. I believe this syntax is easier to remember, with the possible exception of case, which still has some shell legacy.

Spoiler: there will also be expression-based variants of each of these constructs:

if (x > 0) {
  echo hi
}
while (x > 0) {
  echo hi
}
for (x in %(a b c)) {
  echo $x
}

There is probably going to be switch/case or match/case, but that will likely come much later!

That Affect Runtime Behavior

TODO: copy examples from spec tests

echo $dir/*.py

Strict Option Produce More Errors

These options produce more programming errors. Importantly, the resulting program is still compatible with other shells.

For example, shopt -s strict-array produces runtime errors when you confuse strings and arrays. After you fix these problems, your program will still run correctly under bash.

In contrast, if you set shopt -s simple-word-eval (an option that doesn't start with strict-), the semantics of your program have changed, and you can no longer run it under other shells. It's considered an "Oil option": by setting it, you're upgrading to the Oil language.

See the OSH manual for a list of strict options and their meaning.


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