help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] ${var:?message} customization


From: Stephane Chazelas
Subject: Re: [Help-bash] ${var:?message} customization
Date: Thu, 14 May 2015 16:17:22 +0100
User-agent: Mutt/1.5.21 (2010-09-15)

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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]