Why is "${1-"$var"}" (option 6 down below) not mentioned in POSIX?
3
votes
1
answer
418
views
The only reference I could find in the spec is this :
> It would be desirable to include the statement "The characters from an enclosed "${" to the matching '}' shall not be affected by the double-quotes", similar to the one for "$()". However, historical practice in the System V shell prevents this.
The obvious question is: "what is that historical issue" and, more importantly, how does it affect us today ?
At the
C.2.5 Parameters and Variables
heading further down on that page there are a lot of examples of quoted expansions in what seems an exhaustive effort to list all of them, however, nowhere a twice double quoted example appear.
## Description
The issue is related to the outputs of this 6 alternatives of quoting:
- $b
, "$b"
, ${a-$b}
, ${a-"$b"}
"${a-$b}"
and "${a-"$b"}"
The first five are defined by POSIX and some shells agree on the result of such expansions. Note that some values get split on the value of $IFS
:
$ export a=abc; for sh in dash ksh93 bash; do $sh -c 'IFS="bl"; b=value
printf " " 1$b 2"$b" 3${a-$b} 4${a-"$b"} 5"${a-$b}" 6"${a-"$b"}";
echo'; done
However, is "${1-"$var"}"
defined somewhere in the spec ?
That is: with **repeated** double quotes, external and internal.
## Additional 1
Just as related information, look at this results: Note that the only difference of the previous code to this next code is that $a
is unset:
$ unset a; for sh in dash ksh93 bash; do $sh -c 'IFS="bl"; b=value
printf " " 1$b 2"$b" 3${a-$b} 4${a-"$b"} 5"${a-$b}" 6"${a-"$b"}";
echo'; done
## Additional 2
Common shells act in conflicting ways when actually testing all quoting options.
$ for sh in dash ksh93 bash; do $sh -c 'IFS="bl"; b=value
printf " " 1\n 2"\n" 3${a-\n} 4${a-"\n"} 5"${a-\n}" 6"${a-"\n"}";
echo'; done
And that is without including zsh!.
Asked by QuartzCristal
(2113 rep)
Oct 5, 2022, 01:12 PM
Last activity: Nov 18, 2022, 11:06 AM
Last activity: Nov 18, 2022, 11:06 AM