help-bash
[Top][All Lists]
Advanced

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

Re: re-evaluating prompt strings from a bind -x function


From: Christoph Anton Mitterer
Subject: Re: re-evaluating prompt strings from a bind -x function
Date: Tue, 17 Oct 2023 16:14:01 +0200
User-agent: Evolution 3.50.0-1

Hey.


On Fri, 2023-10-06 at 22:14 -0400, Grisha Levit wrote:

> If the variable is readonly you cannot change it in the first place
> so any
> question about restoring it is moot. And since we're talking about a
> function invoked directly from a `bind -x` binding, it's not possible
> for
> the variable to be local to any other function either.

For readonly, yes.
But the function, which is intended to be called from bind -x (where it
should not see local variables) might be still be called manually (e.g.
accidentally by the user) form another function, in which case it
should be able to see local variables.

But declare called in a function causes the var to be local to that
(unless -g is given) so just a plain:
   outer()
   {
        local v=foo
        printf 'outer: before mine ' ; declare -p v
        mine
        printf 'outer: after mine ' ; declare -p v
   }
   
   mine()
   {
        printf 'mine: begin ' ; declare -p v
        local orig="$(declare -p v)"
        printf 'mine: orig = %s\n' "$orig"
        v=bar
        printf 'mine: after set ' ; declare -p v
        eval "$orig"
        printf 'mine: after eval ' ; declare -p v
   }
   
$ outer
outer: before mine declare -- v="foo"
mine: begin declare -- v="foo"
mine: orig = declare -- v="foo"
mine: after set declare -- v="bar"
mine: after eval declare -- v="foo"
outer: after mine declare -- v="bar"

Doesn't work. But even with -g added (e.g. via sed, when getting the
value for orig) it won't work, as that causes it to be declared at the
global scope (i.e. "above" outer's v).


I would conclude from that, that it's impossible to fully restore the
state of a variable, at least if one has unset it or somehow changed
any attributes (like making a previously normal variable an array or
so).

Cause if I want to restore it's attributes (e.g. back to previously
neither -a nor -A from -a, or e.g. from -A to -a) I'd need to unset it,
and by doing that I'd loose any possible local scope.


Thanks,
Chris.



reply via email to

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