bug-bash
[Top][All Lists]
Advanced

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

Re: Return from function depending on number of parameters


From: Greg Wooledge
Subject: Re: Return from function depending on number of parameters
Date: Mon, 6 Jul 2020 07:50:26 -0400
User-agent: Mutt/1.10.1 (2018-07-13)

On Fri, Jul 03, 2020 at 07:00:54PM +0100, Chris Elvidge wrote:
> I've used 'return $((!$#))' and 'return $[!$#]' to return an error if no
> parameters given to function.

The problem with this is that it *always* returns from the function,
even when paramters are given.

If you actually want to do what you said, you need a conditional check
of some kind, either "if" or "||".

(($#)) || return 1

would be my preference, if you really do not want to give any kind of
error message to indicate *why* you are returning with a failure code.

If you want to add an error message, then I would go with "if".

if ((! $#)); then
  echo "usage: myfunc arg1 ..." >&2
  return 1
fi



reply via email to

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