In bash, given a string assignment as follows, how do I "add slashes" automagically, so that it can be safely passed to another program? Notice that the assignment contains spaces, single-quotes and double-quotes, maybe god-only-knows-what-else. It's untrusted data. Yet I need to pass it all *safely*. The appropriate function in PHP is addslashes(); but what is the bash equivalent? EG: #! /bin/sh A="This isn't a \"parameter\""; B=`/path/to/somecommand.sh $A`; exit 0; Thanks, -Ben -- Only those who reach toward a goal are likely to achieve it. -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean.
On Tue, Feb 26, 2008 at 10:11 AM, Benjamin Smith <lists at benjamindsmith.com> wrote:> In bash, given a string assignment as follows, how do I "add slashes" > automagically, so that it can be safely passed to another program? Notice > that the assignment contains spaces, single-quotes and double-quotes, > maybe > god-only-knows-what-else. It's untrusted data. > > Yet I need to pass it all *safely*. > > The appropriate function in PHP is addslashes(); but what is the bash > equivalent? EG: >short answer: single quotes will handle all characters, except single quotes. long answer: man bash the section called QUOTING may help you figure a solution.> > #! /bin/sh > A="This isn't a \"parameter\"";> B=`/path/to/somecommand.sh $A`; > exit 0; > > > Thanks, > > -Ben >HTH, -Bob -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.centos.org/pipermail/centos/attachments/20080226/b81aa880/attachment.html>
On Tuesday 26 February 2008, Bob Beers wrote:> short answer:? single quotes will handle all characters, except singlequotes.> > long answer:? man bash > ?the section called QUOTING may help you figure a solution.I've read the man page. It helps if I already know the input - I don't have a problem with manually putting slashes in front of spaces and single quotes. But in this case, I don't know the input. It's untrusted data. There is no mechanism for escaping untrusted input? -Ben -- -- Only those who reach toward a goal are likely to achieve it. -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean.