help-bash
[Top][All Lists]
Advanced

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

Re: using a variable from global scope or checking whether it is local t


From: Koichi Murase
Subject: Re: using a variable from global scope or checking whether it is local to some scope
Date: Fri, 20 Oct 2023 15:19:28 +0900

2023年10月20日(金) 11:33 Christoph Anton Mitterer <calestyo@scientia.org>:
> > This is implemented in the following function:
> >
> > https://github.com/akinomyoga/ble.sh/blob/742cf2cce6792a9460d38e92f715beb9870a6ff3/src/util.sh#L3031-L3151
> > You can use `ble/util/print-global-definitions varname' from inside a
> > function to get the declaration of the variable. If you want to copy
> > it to the current scope, you may simply run eval
> > "$(ble/util/print-global-definitions varname)".
>
> Won't this fail if the value would end in newlines?

No. The output is declarations. Not values.

> > In a subshell, you can set the readonly attribute to the currently
> > visible variable and try to override it in a child function call. It
> > fails if the variable is a global variable because a global readonly
> > variable is not allowed to be hidden even by a local variable.
>
> So that again requires, that the variable isn't already readonly?!

No. If the variable is already readonly, it is actually already in the
desired state to test if it's global or not. Trying to make an
existing readonly variable readonly does not change anything, so the
desired state is kept.



reply via email to

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