[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: |
Eric Blake |
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:26:26 -0600 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 |
On 03/25/2015 02:57 PM, Peng Yu wrote:
>> printf -v escaped_var %q "$var"
Fork-free.
>
> I don't see this usage in the manual.
It's there in 'man bash':
printf [-v var] format [arguments]
...
%q causes printf to output the corresponding
argument in a
format that can be reused as shell input.
and also in 'help printf'
-v var assign the output to shell variable VAR rather than
display it on the standard output
...
%q quote the argument in a way that can be reused as shell input
>
>> shquote() {
>> local q=\'
>> printf "'%s'" "${1//"$q"/$q\\$q$q}"
>> }
>> escaped_var=$(shquote "$var")
Requires a fork for the command substitution.
>
> This is not robust with other characters that need to be escaped.
How so? Single quote is the only character that needs to be
special-cased when using single quoting. What characters are you
thinking of?
Or are you trying to escape a string to be suitable for use within ""
instead of within '', as was done here? If so, then you have a bit more
work to do (\"$` are the four characters that need escaping), but it is
still doable. But why bother, when single quoting is easier?
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature
- [Help-bash] Transform strings with special characters so that the strings don't need to be quoted?, Peng Yu, 2015/03/24
- Re: [Help-bash] Transform strings with special characters so that the strings don't need to be quoted?, Stephane Chazelas, 2015/03/29
- Re: [Help-bash] Transform strings with special characters so that the strings don't need to be quoted?, Greg Wooledge, 2015/03/30
- Re: [Help-bash] Transform strings with special characters so that the strings don't need to be quoted?, Stephane Chazelas, 2015/03/30
- Re: [Help-bash] Transform strings with special characters so that the strings don't need to be quoted?, Greg Wooledge, 2015/03/30
- Re: [Help-bash] Transform strings with special characters so that the strings don't need to be quoted?, Stephane Chazelas, 2015/03/30
- Re: [Help-bash] Transform strings with special characters so that the strings don't need to be quoted?, Greg Wooledge, 2015/03/30
- Re: [Help-bash] Transform strings with special characters so that the strings don't need to be quoted?, Stephane Chazelas, 2015/03/30