help-bash
[Top][All Lists]
Advanced

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

Re: Is there a good way to tell the difference between a variable declar


From: Peng Yu
Subject: Re: Is there a good way to tell the difference between a variable declared in a function then a variable declared outside a function?
Date: Sat, 29 Aug 2020 23:38:12 -0500

I don't a way like `declare -p` which return 0 (when the vairable is
defined in a function) and return non-zero (when the variable is not
defined in a function). I don't think `local` can do so.

On 8/29/20, Marco Ippolito <maroloccio@gmail.com> wrote:
> On 30/08/2020 01:18, Peng Yu wrote:
>> Hi,
>>
>> `declare -p` can tell whether a variable is declared. But it can not
>> tell whether the variable is declared with the current function or
>> outside the current function. Is there a way to do so? Thanks.
>
> If it appears when executing "local", it was defined in the current
> function.
>
> ~~~~~~~
> Reference: local
>
> With no operands, local writes a list of local variables to the standard
> output.  It is an error to use local when  not  within  a  function.
> ~~~~~~~
>
> Specifically, it foo was defined as a local in func1 and func1 calls
> func2, "$foo" will be "usable" in func2 but foo will not appear when you
> execute "local" from inside func2.
>
> What is your use case for this question, please?
>
> Marco Ippolito
> maroloccio@gmail.com
>
>


-- 
Regards,
Peng



reply via email to

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