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: Eli Schwartz
Subject: Re: Return from function depending on number of parameters
Date: Sat, 4 Jul 2020 23:35:57 -0400
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.9.0

On 7/4/20 7:54 AM, Chris Elvidge wrote:
> On 03/07/2020 11:16 pm, Eli Schwartz wrote:
>> 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".)
>>
> 
> Thanks for the pointers to a better bug report.
> And thanks for the info on histexpand. I obviously should read the
> manual more carefully - I'd never come across it - as it's set by
> default, not in any profile or bashrc file.

It's the section "HISTORY EXPANSION", and set -o histexpand is
equivalent to set -H, which controls this option.

Many people first realize it exists only when they've been bitten by it. :)

> Did you mean set +o histexpand to disable the feature?

"set +o histexpand" is in my bashrc to disable the feature.

"set -o histexpand" is what I used at the console to temporarily
re-enable it, in order to reproduce your issue. As you can see from

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

the first history expansion inserted the word "histexpand" into my
command, because the previous command used the word.

-- 
Eli Schwartz
Arch Linux Bug Wrangler and Trusted User

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

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