Oil Doesn't Require Quoting Everywhere
source link: http://www.oilshell.org/blog/2021/04/simple-word-eval.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
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.
Context:
How to do things safely in Bash (2018) (github.com/anordal
via Hacker News)
173 points, 94 comments - 6 days ago
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.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK