Skip to content

Gradually Upgrading Shell to Oil

andychu edited this page May 16, 2021 · 25 revisions

The slogan of Oil is that it's *our upgrade path from bash to a better language and runtime.

How exactly does that work? This page will give you an idea. Written in response to this lobste.rs thread.

Step 1: Run osh myscript.sh

Your shell script should run as is. You most likely won't notice a difference, except perhaps better error messages when the script fails. (If you want gory details, see the Known Differences doc, but again these differences are small and rare.)

Optional: change the shebang line to something like #!/usr/local/bin/osh.

New Features

You can now start using features like var, const, setvar, and proc. You can just write them anywhere in the middle of your shell program!

const name = 'World'
var x = {greeting: "Hello $name"}  # A Python/JavaScript-like dictionary, with shell strings!

proc mycopy(src, dest) {        # named parameters!
  cp --verbose "$src" "$dest"   # You still need this quoting, which is ugly
}

mycopy mytest /tmp              # a proc is just like a shell function

(Note: early in Oil's life, the project was aiming for automatic translation from bash to Oil. This is no longer a goal, although the work could be revived by an interested party.)

Step 2: Add shopt --set oil:basic To the Top of Your Program

This breaks a few things, but opens up more features.

See Shell Language Deprecations

The most common one is probably subshell:

(cd /tmp; ls -l)             # No longer valid

forkwait { cd /tmp; ls -l }  # the new way to write subshells

cd /tmp { ls -l }            # Better

In exchange for changing your subshell syntax, you get Python-like expressions in if and while:

if (x > 0) {
  echo "$x is positive"
}

You don't have to remember how to type [ "$x" -gt 0 ] or [[ $x -gt 0 ]] or test "$x" -gt 0, or the differences between them. Oil has Python-like expressions.

Other changes:

  • One of the biggest changes is Simple Word Evaluation. This means you can now write cp --verbose $src $dest without the quotes.
    • You can also splice an array with cp --verbose @list_of_files $dest. This doesn't mangle your filenames!
  • If you had something like echo @foo in your shell script, you should change it to echo '@foo' to avoid invoking splicing. The @ is only special at the beginning of a word.

Step 3: Run with bin/oil

This breaks more things, and is still subject to change. That said, I encourage you to try it and tell me what happens! Dozens of people have given feedback on the language and improved it.

Where To Send Feedback

Related

Clone this wiki locally