On 7/18/18 8:25 AM, Denys Vlasenko wrote: > Unquoted ${v:+ARG} operator allows single-quoted strings > in ARG: > > $ x=a; echo ${x:+'b c'} > b c > > In this case, obviously single quotes must be paired. > > If ${v:+ARG} is in double-quoted string, single quotes lose their special > status: > > $ x=a; echo "${x:+'b c'}" > 'b c' > > IOW: they work similarly to ordinary dquoted strings: echo "It's sunny" > treats single quote as ordinary char. > > However, bash errors out if they are not paired: > > $ x=a; echo "${x:+'b c}" >> <------------- bash thinks 'str' did not end yet
It's ambiguous, and the standard behavior changed in early 2010, so bash does it the way you want in posix mode. The history is complicated. The POSIX standard says, in section 2.2.3: "Within the string of characters from an enclosed "${" to the matching '}', an even number of unescaped double-quotes or single-quotes, if any, shall occur." http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_02_03 That could mean that it's a restriction on the application, but the standard doesn't say that when it does say it in other places. The word `unescaped' could be taken to mean that the outer double quotes escape the single quote and remove the restriction, but that doesn't make sense in the context of describing the effects of double quotes. Shells, including bash and ksh93, used to interpret this to mean that the single quotes had to be balanced inside double-quoted ${...}. There was a posix interpretation on this issue http://austingroupbugs.net/view.php?id=221 that comes closer to your interpretation, but it's explicitly inconsistent: some expansions treat single quotes specially, and some don't. Bash adheres to this interpretation when in posix mode and uses its historical behavior when not. Chet -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRU c...@case.edu http://tiswww.cwru.edu/~chet/