Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #16531 > unrolled thread

Re: Return from function depending on number of parameters

Started byEli Schwartz <eschwartz@archlinux.org>
First post2020-07-03 18:16 -0400
Last post2020-07-03 18:16 -0400
Articles 1 — 1 participant

Back to article view | Back to gnu.bash.bug

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Return from function depending on number of parameters Eli Schwartz <eschwartz@archlinux.org> - 2020-07-03 18:16 -0400

#16531 — Re: Return from function depending on number of parameters

FromEli Schwartz <eschwartz@archlinux.org>
Date2020-07-03 18:16 -0400
SubjectRe: Return from function depending on number of parameters
Message-ID<mailman.910.1593814610.2574.bug-bash@gnu.org>

[Multipart message — attachments visible in raw view] — view raw

On 7/3/20 2:00 PM, Chris Elvidge wrote:
> I've used 'return $((!$#))' and 'return $[!$#]' to return an error if no
> parameters given to function.
> 
> Tested in a bash script 'exit $((!$#)) / $[!$#]' - both work.
> 
> 'echo  $((!$#)) / $[!$#]' - both echo 1 when no params, 0 when any
> number of params.
> 
> I'm told ( https://wiki.bash-hackers.org/scripting/obsolete ) that
> $[...] is obsolete and that $((...)) should be used instead. OK so far.
> 
> However 'N=0; echo $((!$N))' gives an error at the bash prompt. 'echo
> $[!$N]' echo's 1 as expected.

"gives an error" is a useless bug report. It works for me.

$ N=0; echo $((!$N))
1

My initial reaction to reading this thread is head scratching!

As the other reply mentioned, there's actually a good explanation for
why we get different results -- I disabled an annoying feature.

$ set -o histexpand

Now here's a useful bug report. "When I run this, I get the following
incorrect results or error message":

$ N=0; echo $((!$N))
N=0; echo $((histexpandN))
0
$ N=0; echo $((!$N))
N=0; echo $(()N))
-bash: syntax error near unexpected token `)'
$ N=0
$ echo $((!$N))
echo $((N=0N))
-bash: N=0N: value too great for base (error token is "0N")

...

From there, people can give useful advice for solving the problem. (My
preferred advice is "disable histexpand".)

-- 
Eli Schwartz
Arch Linux Bug Wrangler and Trusted User

[toc] | [standalone]


Back to top | Article view | gnu.bash.bug


csiph-web