Discussion:
remove empty '' in ${var@Q} result?
Clark Wang
2017-10-30 02:34:47 UTC
Permalink
See following example:

[STEP 100] # echo $BASH_VERSION
4.4.12(2)-release
[STEP 101] # v=\'\'
[STEP 102] # printf '%q\n' "$v"
\'\'
[STEP 103] # printf '%s\n' "${***@Q}"
''\'''\'''
[STEP 104] #
Greg Wooledge
2017-10-30 12:35:26 UTC
Permalink
Post by Clark Wang
[STEP 100] # echo $BASH_VERSION
4.4.12(2)-release
[STEP 101] # v=\'\'
[STEP 102] # printf '%q\n' "$v"
\'\'
''\'''\'''
[STEP 104] #
What's the bug? They are equivalent.

If you mean "I would like bash to perform a second optimization pass
over the result of ${***@Q} so that it's prettier in my degenerate edge
cases", I suspect there are better uses of Chet's time, but it's his call.
Clark Wang
2017-11-08 04:36:21 UTC
Permalink
Post by Greg Wooledge
What's the bug? They are equivalent.
It's not a bad thing if we can make the language a bit more elegant unless
the cost is not worth it.
Post by Greg Wooledge
If you mean "I would like bash to perform a second optimization pass
cases", I suspect there are better uses of Chet's time, but it's his call.
It's not necessarily to perform a second pass parsing.

-clark
Chet Ramey
2017-10-30 14:41:02 UTC
Permalink
Post by Clark Wang
[STEP 100] # echo $BASH_VERSION
4.4.12(2)-release
[STEP 101] # v=\'\'
[STEP 102] # printf '%q\n' "$v"
\'\'
''\'''\'''
[STEP 104] #
This is an effect of using single quotes in the @Q operator. If you want
to single-quote a string containing single quotes, this is how you do it.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU ***@case.edu http://cnswww.cns.cwru.edu/~chet/
Clark Wang
2017-10-31 07:25:12 UTC
Permalink
Post by Chet Ramey
to single-quote a string containing single quotes, this is how you do it.
I made a patch (also attached). Please see if it's ok.

Tested with arr=('' a \' \'\' \'\'\' \'a a\' \'a\'a). the result will be:

arr[0]=''
arr[1]='a'
arr[2]=\'
arr[3]=\'\'
arr[4]=\'\'\'
arr[5]=\''a'
arr[6]='a'\'
arr[7]=\''a'\''a'

--- a/lib/sh/shquote.c
+++ b/lib/sh/shquote.c
@@ -98,34 +98,32 @@ sh_single_quote (string)
register int c;
char *result, *r;
const char *s;
+ int left_quote_inserted = 0;

- result = (char *)xmalloc (3 + (4 * strlen (string)));
+ result = (char *)xmalloc (1 + (3 * strlen (string)));
r = result;

- if (string[0] == '\'' && string[1] == 0)
- {
- *r++ = '\\';
- *r++ = '\'';
- *r++ = 0;
- return result;
- }
-
- *r++ = '\'';
-
for (s = string; s && (c = *s); s++)
{
- *r++ = c;
-
- if (c == '\'')
- {
- *r++ = '\\'; /* insert escaped single quote */
+ if (c == '\'') {
+ if (left_quote_inserted) {
+ *r++ = '\'';
+ left_quote_inserted = 0;
+ }
+ *r++ = '\\';
+ *r++ = '\'';
+ } else {
+ if ( ! left_quote_inserted) {
*r++ = '\'';
- *r++ = '\''; /* start new quoted string */
+ left_quote_inserted = 1;
}
+ *r++ = c;
+ }
}
-
- *r++ = '\'';
- *r = '\0';
+ if (left_quote_inserted) {
+ *r++ = '\'';
+ }
+ *r++ = 0;

return (result);
}
Clark Wang
2017-10-31 07:53:34 UTC
Permalink
Post by Clark Wang
Post by Chet Ramey
to single-quote a string containing single quotes, this is how you do it.
I made a patch (also attached). Please see if it's ok.
Updated by dealing with empty strings (and malloc'ing 2 more bytes) though
I'm not sure if it's necessary since the func sh_quote_reusable() already
handles empty strings.
Clark Wang
2017-11-08 04:38:26 UTC
Permalink
Post by Clark Wang
Post by Clark Wang
Post by Chet Ramey
to single-quote a string containing single quotes, this is how you do it.
I made a patch (also attached). Please see if it's ok.
Updated by dealing with empty strings (and malloc'ing 2 more bytes) though
I'm not sure if it's necessary since the func sh_quote_reusable() already
handles empty strings.
Hi Chet, do you have a look at my patch?
Chet Ramey
2017-11-08 13:46:38 UTC
Permalink
Post by Clark Wang
I made a patch (also attached). Please see if it's ok.
Updated by dealing with empty strings (and malloc'ing 2 more bytes)
though I'm not sure if it's necessary since the func
sh_quote_reusable() already handles empty strings.
Hi Chet, do you have a look at my patch?
I did. It's on the list of possible things for the next version. Since it's
only a cosmetic issue, it's not a high priority.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU ***@case.edu http://cnswww.cns.cwru.edu/~chet/
Clark Wang
2018-11-29 02:58:19 UTC
Permalink
Post by Chet Ramey
Post by Clark Wang
I made a patch (also attached). Please see if it's ok.
Updated by dealing with empty strings (and malloc'ing 2 more bytes)
though I'm not sure if it's necessary since the func
sh_quote_reusable() already handles empty strings.
Hi Chet, do you have a look at my patch?
I did. It's on the list of possible things for the next version. Since it's
only a cosmetic issue, it's not a high priority.
Hi Chet,

Is it possible to make the change in the coming 5.0 release?

-clark
Chet Ramey
2018-12-03 15:30:40 UTC
Permalink
Post by Chet Ramey
         I made a patch (also attached). Please see if it's ok.
     Updated by dealing with empty strings (and malloc'ing 2 more bytes)
     though I'm not sure if it's necessary since the func
     sh_quote_reusable() already handles empty strings.
Hi Chet, do you have a look at my patch?
I did. It's on the list of possible things for the next version. Since it's
only a cosmetic issue, it's not a high priority.
Hi Chet,
Is it possible to make the change in the coming 5.0 release?
I am not making changes for bash-5.0 at this point, only bug fixes.

Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU ***@case.edu http://tiswww.cwru.edu/~chet/
Loading...