bug-bash
[Top][All Lists]
Advanced

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

Re: $@ in function gives error


From: Lawrence Velázquez
Subject: Re: $@ in function gives error
Date: Fri, 16 Aug 2024 18:47:12 -0400

On Fri, Aug 16, 2024, at 6:29 PM, Lawrence Velázquez wrote:
> There is no problem with "$@" or functions here.  The "problem" is
> that "$@" expands to multiple fields when there are two or more
> positional parameters, so (as the error message says) you end up
> running test(1) with too many arguments.  This is a usage error.
>
> [...]
>
> What are you trying to test for, anyway?  Whether there are any
> positional parameters?  The latter test (which you claim "works
> OK") does succeed when there are nonempty positional parameters,
> but it also succeeds when there are multiple parameters that are
> all empty, and it fails when there is a single empty parameter.

Also note that when there are no positional parameters, "$@" expands
to *zero* fields, breaking the first test in a different way.

        $ set -x --
        $ test -n "$@"; : "$?"
        + test -n
        + : 0

It succeeds because "-n" is the only argument, and test(1) always
succeeds when invoked with a single nonempty argument.

-- 
vq



reply via email to

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