tech-userlevel archive

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

Re: Add static local vars to sh(1) ?



On Mon, Jan 29, 2018 at 23:33:25 +0700, Robert Elz wrote:

>   From: uwe%stderr.spb.ru@localhost (Valery Ushakov)
>   Subject: Re: Add static local vars to sh(1) ?
>   Date: Mon, 29 Jan 2018 13:38:25 +0000 (UTC)
> 
>   | This doesn't seem to mention what happens when the
>   | function is called recursively.
[...]
>   | I assume it does the right thing,
> 
> In considering what the right thing is, please remember that sh is
> not C, its language is not C, and most especially, its variables
> are nothing like what exists in C (or other declarative programming
> languages).
> 
> In particular, a static variable in sh will not (and cannot)
> be private to a function, the way it would be in C.
[...]
> Then local -S causes any of that init to only happen when the func
> is executed the first time (really, when the "local -S" is executed in
> this func for the very first time).  After that you get whatever value
> the variable (the version of it available in this function after the
> local command) last had.  It is essentially inherited from itself.
> Changing the var init on local that way, and arranging to preserve
> the return time value of a static local, so it is not lost like a
> normal local var's value would be, is really all that is happening here.
> 
> kre
> 
> ps: given everything in sh is dynamic, defining functions, and
> the local command included, and you can achieve some truly
> bizarre effects by exploiting that if so inclined.  Eg: by
> only sometimes making a var static in the function...
> 
> And lastly, apologies for being too classroomish, expecially
> given I know that most of you know all of this already.

Thanks for the master-class :) So "local" is like in lisp using LET on
a dynamically scoped (aka special) variable.  I never ventured into
that particular deep end of shell scripting.  Thanks again for the
clarification.

-uwe


Home | Main Index | Thread Index | Old Index