zsh-users
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Re: unexpected unmodified variable
Date: Thu, 06 Oct 2022 20:28:08 +0100	[thread overview]
Message-ID: <3bf17a2cc56cbc65fc0ba855fe9c7d2d2f63befb.camel@ntlworld.com> (raw)
In-Reply-To: <1774a1bc-c353-4e16-80a2-0082457de6e0@eastlink.ca>

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



  reply	other threads:[~2022-10-06 19:29 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-06 19:07 Ray Andrews
2022-10-06 19:28 ` Peter Stephenson [this message]
2022-10-06 21:58   ` Ray Andrews
2022-10-07  0:36     ` Bart Schaefer
2022-10-07  1:37       ` Ray Andrews
2022-10-07  4:36         ` Bart Schaefer
2022-10-07 15:28           ` Ray Andrews
2022-10-07 20:00             ` Bart Schaefer
2022-10-07 21:59               ` Ray Andrews

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3bf17a2cc56cbc65fc0ba855fe9c7d2d2f63befb.camel@ntlworld.com \
    --to=p.w.stephenson@ntlworld.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).