[Top][All Lists]

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

Re: [bash 4] 'test -v 1' is never true

From: Robert Elz
Subject: Re: [bash 4] 'test -v 1' is never true
Date: Sat, 26 Nov 2022 10:09:22 +0700

    Date:        Fri, 25 Nov 2022 16:53:11 -0500
    From:        Greg Wooledge <greg@wooledge.org>
    Message-ID:  <Y4E5R6FCwdqa9hd0@wooledge.org>

  | So the question is whether "1" qualifies as a "shell variable".  I would
  | argue that it doesn't -- it's a special parameter, not a variable (because
  | you can't assign to it).

A positional param, rather than a special param, but the same argument

  | So, there's no reason to expect that "test -v 1" would work.

And even with variables, is non portable, and as alternatives
exist here which are portable and just as easy to use, why not
just use one of thise, such as:

  | you should use something like:
  |     test "$#" -gt 0

but only for positional params (the 0 is 1 less than the
positional param to be checked ... for positional params,
if one exists (is set), all those before it are also set.
This can be easier to read if written as 'test "$@" -ge 1'
which means the same.

My preferred method for this, which works for all kinds
of variables and parameters is

        test "${X+s}" = s

where X is whatever is to be tested (1 in this case)
and "s" is any string you like, except "" .. must be
the same both occurrences of course.


reply via email to

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