help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Transform strings with special characters so that the st


From: Bob Proulx
Subject: Re: [Help-bash] Transform strings with special characters so that the strings don't need to be quoted?
Date: Wed, 25 Mar 2015 15:21:30 -0600
User-agent: Mutt/1.5.23 (2014-03-12)

Peng Yu wrote:
> > printf -v escaped_var %q "$var"
> 
> I don't see this usage in the manual.

       printf [-v var] format [arguments]
              Write the formatted arguments to the standard output under the
              control of the format.  The -v option causes the output to be
              assigned to the variable var rather than being printed to the
              standard output.
  ...
              %q     causes printf to output the corresponding argument in a
                     format that can be reused as shell input.

> > shquote() {
> >   local q=\'
> >   printf "'%s'" "${1//"$q"/$q\\$q$q}"
> > }
> > escaped_var=$(shquote "$var")
> 
> This is not robust with other characters that need to be escaped.

Example?

  "Few things are harder to put up with than the annoyance of a good
  example." -- Mark Twain

Bob



reply via email to

[Prev in Thread] Current Thread [Next in Thread]