2015-05-14 07:06:51 -0600, Eric Blake:
> On 05/14/2015 06:23 AM, Greg Wooledge wrote:
> > If you want to distinguish between "unset" and "set to an empty string",
> > you can use a more subtle test:
> >
> > if [ "${var+defined}" ]; then
>
> By the way, this test can be portably shortened to:
>
> if [ ${var+y} ]; then
That assumes $IFS doesn't contain y. If it does, then depending
on the shell, if $var is set, that's:
[ ]
[ "" ]
or
[ "" "" ]
All of which mean false but the last one will give an error.
> (one of the few places where unquoted variable expansion actually does
> the right thing, even on ancient shells)
It does not in those cases and there's no reason why you
wouldn't quote it. Not-quoting it is still asking the shell to
split+glob it.
test "${var+y}"
would be portable.
>
> > if [ -z "$var" ]; then
>
> Conversely, while this is portable to POSIX shells, it is NOT portable
> to some older implementations of /bin/sh (where there are certain values
> of $var such as ")" that cause [ to misbehave).
[...]
In that case, that's more values like "=".
Using case here would be a way to avoid quoting if you don't
like quotes:
case ${var+y} in
y) echo set
esac
--
Stephane