blog |

Oil Doesn't Require Quoting Everywhere


I published the reference doc Simple Word Evaluation in Unix Shell last year, describing an important improvement in Oil.

It also needs a short, friendly explanation, and this comment by Hacker News user nas prompted me write one:

The fact that you have to use quoting nearly everywhere is a design flaw in the Bourne shell. Some shells, like Plan 9's rc, for example, don't expand after variable [substitution]. They have an operator to call if you want to explicitly force expansion. That's so much cleaner and less error prone.


Oil's Solution

Oil is Bourne compatible, but has a mode to opt you into the better behavior. Here's an example, starting with 2 string variables:

osh$ empty=''
osh$ x='name with spaces.mp3'

This behavior matches Bourne shell:

$ argv $empty $x
['name', 'with', 'spaces.mp3']  # omit empty and split

$ argv "$empty" "$x"       
['', 'name with spaces.mp3']    # unchanged due to quotes

But you can opt into better behavior (also available with bin/oil):

$ shopt --set oil:basic

Which lets you omit the quotes:

$ argv $empty $x                                                                                 
['', 'name with spaces.mp3']  # no splitting or omission

If you want splitting, splice the result of the split() function into the command, with the @ operator:

$ argv $empty @split(x)
['', 'name', 'with', 'spaces.mp3']

If you want to omit empty strings, use the maybe() function, which returns a 0 or 1 length array:

$ argv @maybe(empty) $x
['name with spaces.mp3']

Again, this is called Simple Word Evaluation, and it's exactly what the comment asked for. Let me know if you have questions!

Past Posts

Also, see other posts tagged #real-problems for Oil's solutions to problems that come up in practice.