Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Robust parameter quoting for *nixoid shells

by Aristotle (Chancellor)
on Apr 26, 2003 at 00:53 UTC ( #253295=snippet: print w/ replies, xml ) Need Help??

Description:

Eventually, everyone working in a *nix environment will have to pass things to the shell, but as everyone should be aware of by now, most homegrown quoting solutions are inadequate and breakable. I spent some effort researching semantics to write one that isnít. The only situation this snippet canít cope with is null bytes, because the shell eats those without providing any way to preserve them.

Itís likely to be less efficient than substitution based approaches, but I also find it a lot clearer and cleaner. Regex-based solutions feel to me like careful fiddling. This one is easy to grok, and any edge cases would be easily apparent (there arenít any); when it comes to a job as critical as this one Iíll take the obviously deficiency-free approach over the fast one any day.

sub shell_quote {
    return join ' ', map {
        ( defined && length )
            ? join q[\\'], map { (length) ? qq['$_'] : q[] } split /'/
+, $_, -1
            : q[''];
    } @_;
}
Comment on Robust parameter quoting for *nixoid shells
Download Code

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://253295]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (13)
As of 2014-09-22 17:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (198 votes), past polls