bug-bash
[Top][All Lists]
Advanced

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

Re: Should [[ -v 1 ]] be supported?


From: Peng Yu
Subject: Re: Should [[ -v 1 ]] be supported?
Date: Thu, 27 Dec 2018 18:39:26 -0600

On Thu, Dec 27, 2018 at 3:19 PM Martijn Dekker <address@hidden> wrote:
>
> Op 27-12-18 om 19:22 schreef Chet Ramey:
> > On 12/26/18 10:49 PM, Peng Yu wrote:
> >
> >> Although [[ -z ${1+s} ]]  and (($#)) works for testing if $1 is set,
> >> neither of them are uniformly better performance wise. In this case,
> >> should [[ -v 1 ]] be supported?
> >
> > So you're saying that neither of the existing options performs better
> > than the other, though they both perform well, so we should add some
> > new capability just because? That's a particularly poor argument.
>
> Consistency might be a better argument. If [[ -v foo ]] is equivalent to
> [[ -n ${foo+s} ]] for variables (with the advantage that you don't need
> 'eval' to handle arbitrary values of 'foo'), then perhaps it's not
> unreasonable to expect [[ -v 1 ]] to be equivalent to [[ -n ${1+s} ]].
>
> FWIW, zsh and mksh do support this; ksh93 doesn't.

The above are additions arguments that why [[ -v 1 ]] should be supported.

What I meant in my original email is that I want something for testing
if there is a command line argument (one or more, the exact number
does not matter). $# gives more than that info, because it tells not
only whether is any command line argument, but also how many. This
could lead to slower performance if the goal is to just test if there
is an argument.

[[ -z ${1+s} ]] does something also more than necessary too, because
it not only tests for whether $1 is set, it also replaced with a
string "s". This also does more than just testing whether $1 is set.

So both cases would be slower than [[ -v 1 ]] if it were supported.

As of now, because (($#)) or [[ -z ${1+s} ]] are not consistently
faster than the other, there is no way to write a program that is
consistently fastest. To achieve this goal, one has to implement [[ -v
1 ]] or something similar that just test whether $1 but no more.

-- 
Regards,
Peng



reply via email to

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