this all started with a wish to single quote a variable. Doesn't matter
why I have multiple solutions to that now.
But it it an interesting problem for exploring how escaping works in
variable expansion.
so for the test case the goal is to take a string like
kljlksdjflsd'jkjkljl
wrap it with single quotes and globally replace all single quotes in the
string with '\''
its a workaround because it doesn't work all the time you would need
something more like this
IFS= echo \'${test//"'"/\'\\\'\'}\'" "
'weferfds'\''dsfsdf'
On 02/28/2012 05:01 PM, Greg Wooledge wrote:
> On Tue, Feb 28, 2012 at 04:52:48PM +0100, John Kearney wrote:
>> The standard work around you see is
>> echo -n \'${1//\'/\'\\\'\'}\'" "
>> but its not the same thing
>
> Workaround for what? Not the same thing as what? What is this pile
> of punctuation attempting to do?
>
>> # why does this work, this list was born of frustration, I tried
>> everything I could think of.
>> echo \'${test//"'"/\'\\\'\'}\'" "
>> 'weferfds'\''dsfsdf'
>
> Are you trying to produce "safely usable" strings that can be fed to
> eval later? Use printf %q for that.
>
> imadev:~$ input="ain't it * a \"pickle\"?"
> imadev:~$ printf '%q\n' "$input"
> ain\'t\ it\ \*\ a\ \"pickle\"\?
>
> printf -v evalable_input %q "$input"
>
> Or, y'know, avoid eval.
>
> Or is this something to do with sed? Feeding strings to sed when you
> can't choose a safe delimiter? That would involve an entirely different
> solution. It would be nice to know what the problem is.