[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: |
Fri, 15 May 2015 11:55:31 +0100 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
2015-05-14 17:30:03 -0500, Dan Douglas:
> On Thu, May 14, 2015 at 8:06 AM, Eric Blake <address@hidden> wrote:
> > By the way, this test can be portably shortened to:
> >
> > if [ ${var+y} ]; then
>
> This has always seemed convoluted to me. Testing whether a variable is
> set, only to expand a string and copy its value so another command can
> test the length and get an answer as a side-effect. The simplest and
> fastest POSIX methods are:
>
> ! ${var+false}
> ${var+:} false
>
> Particularly the former since no command is executed at all in the
> unset case, though the latter has the advantage of being
> bourne-compatible.
That assumes $IFS doesn't contain f, a, l, s, e or :.
! ${var+"false"}
${var+":"} false
is as portable and makes it independant from the value of $IFS
except in the Bourne shell where you can't really change IFS
without having to quote everything anyway and except in old
versions of zsh in zsh emulation (where split+glob still applies
on the result of that expansion).
Using ${var+false} unquoted in list context is asking the shell
to split+glob it in all shells.
! ${var+"false"}
does look more convoluted to me than
[ -n "${var+defined}" ]
or
[ "${var+defined}" = defined ]
or
[ "${var+defined}" ]
It takes me longer to decipher.
Note that there's no shell that I know nowadays where "[" is not
builtin so I don't think the performance aspect holds.
--
Stephane
>
>
Re: [Help-bash] ${var:?message} customization, Matias A. Fonzo, 2015/05/14
Re: [Help-bash] ${var:?message} customization, Dan Douglas, 2015/05/14