Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: unexpected unmodified variable



On Thu, 2022-10-06 at 12:07 -0700, Ray Andrews wrote:
> ... This catches me by surprise; when " var =$( func2 ) " happens I'm 
> expecting 'func2' to do everything it does including reset 'count' to 
> '2'.  Why doesn't it, and is there any way to solve that?

$(...) causes a shell to be forked --- it runs in a different process,
prints its output to somewhere that's collected by the main shell,
then exits.  So the internal effects disapper.

There's no simple workaround if you want the output directly other than
what you tried.

You can make the function set a variable instead.

% foo() { typeset -g $1="this is returned from foo"; }
% foo myvar
% print $myvar
this is returned from foo

The -g in the typeset tells it not to create a local scope within foo.

Returning stuff in a sane way is a real weakness in shells.

pws





Messages sorted by: Reverse Date, Date, Thread, Author