zsh-users
 help / color / mirror / code / Atom feed
* Global And Local Variables in zsh Functions
@ 2015-08-08 22:44 Georg Wittig
  2015-08-09  0:26 ` ZyX
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Georg Wittig @ 2015-08-08 22:44 UTC (permalink / raw)
  To: zsh-users

Hi,

I'm writing a zsh script that's constructing a rather complex rsync
command. One part of the script is constructing  a list of exclude
commands for rsync. For this I use a global variable EXC. It is built
in the following way

EXC=''
function my_f1 () {
  EXC+=" --exclude='$1'";
}
my_f1 /tmp
my_f1 /opt/windows7
echo ">>>$EXC<<<"

Function my_f1 does a lot of other things that are not relevant in
this context. The output of that script snippet is

>>> --exclude='/tmp' --exclude='/opt/windows7'<<<

which is exactly what I want.

Now I need that function my_f1 return another value that depends on
it's input parameter. So I rewrote my_f1 to my_f2 which looks as follows

EXC=''
function my_f2 () {
    EXC+=" --exclude='$1'";
    local x="some value depending on $1"
    echo $x
}
x1=$(my_f2 /tmp)
x2=$(my_f2 /opt/windows7)
echo ">>>$EXC<<<"

To my surprise, EXC is empty now: The output is

>>><<<

Why is EXC empty in the case of my_f2, and correct in the case my_f1?
Exists there an influence of the local variable x, or is the culprit
the way that my_f2 returns it's value to the calling point? How do I
rewrite my_f2 such that the value of EXC is correct?

Thanks for your hints,
     --Georg

[zsh-5.0.8-5.fc22.x86_64]


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Global And Local Variables in zsh Functions
  2015-08-08 22:44 Global And Local Variables in zsh Functions Georg Wittig
@ 2015-08-09  0:26 ` ZyX
  2015-08-09  0:31 ` Bart Schaefer
  2015-08-09  0:45 ` Mikael Magnusson
  2 siblings, 0 replies; 4+ messages in thread
From: ZyX @ 2015-08-09  0:26 UTC (permalink / raw)
  To: Georg Wittig, zsh-users



09.08.2015, 01:46, "Georg Wittig" <nc-wittigge@netcologne.de>:
> Hi,
>
> I'm writing a zsh script that's constructing a rather complex rsync
> command. One part of the script is constructing a list of exclude
> commands for rsync. For this I use a global variable EXC. It is built
> in the following way
>
> EXC=''
> function my_f1 () {
>   EXC+=" --exclude='$1'";
> }
> my_f1 /tmp
> my_f1 /opt/windows7

Here you run my_f1 in the current shell.

> echo ">>>$EXC<<<"
>
> Function my_f1 does a lot of other things that are not relevant in
> this context. The output of that script snippet is
>
>>>>  --exclude='/tmp' --exclude='/opt/windows7'<<<
>
> which is exactly what I want.
>
> Now I need that function my_f1 return another value that depends on
> it's input parameter. So I rewrote my_f1 to my_f2 which looks as follows
>
> EXC=''
> function my_f2 () {
>     EXC+=" --exclude='$1'";
>     local x="some value depending on $1"
>     echo $x
> }
> x1=$(my_f2 /tmp)
> x2=$(my_f2 /opt/windows7)

And here you do it in the subshell.

> echo ">>>$EXC<<<"

And then try to use $EXC modified separately two times in two different subshells in a parent shell. Of course, this does not work.

>
> To my surprise, EXC is empty now: The output is
>
>>>> <<<
>
> Why is EXC empty in the case of my_f2, and correct in the case my_f1?
> Exists there an influence of the local variable x, or is the culprit
> the way that my_f2 returns it's value to the calling point? How do I
> rewrite my_f2 such that the value of EXC is correct?

You must not use subshells: in this case $(). Use variable for return.

Note: you may define a local variable and modify it in the called function:

    function foo() {
        REPLY="some value depending on $1"
    }
    REPLY=Test
    () {
        local REPLY
        foo Test
        echo $REPLY  # Will echo "some value depending on Test"
    }
    echo $REPLY  # Will echo "Test"

Note: REPLY is not a random name, it is rather common idiom. Search `man zshall` for the `reply` and `REPLY` variables.

>
> Thanks for your hints,
>      --Georg
>
> [zsh-5.0.8-5.fc22.x86_64]


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Global And Local Variables in zsh Functions
  2015-08-08 22:44 Global And Local Variables in zsh Functions Georg Wittig
  2015-08-09  0:26 ` ZyX
@ 2015-08-09  0:31 ` Bart Schaefer
  2015-08-09  0:45 ` Mikael Magnusson
  2 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2015-08-09  0:31 UTC (permalink / raw)
  To: zsh-users

On Aug 9, 12:44am, Georg Wittig wrote:
}
} x1=$(my_f2 /tmp)
} x2=$(my_f2 /opt/windows7)
} 
} To my surprise, EXC is empty now:
} 
} Why is EXC empty in the case of my_f2, and correct in the case my_f1?

The use of $(...) means your my_f2 function runs in a forked child process,
and so changes to variables are not visible to the parent shell.  If you
want my_f2 to work like my_f1, you'll have to do something like

function my_f2 () {
    EXC+=" --exclude='$1'";
    typeset -g $2="some value depending on $1"
}

my_f2 /tmp x1
my_f2 /opt/windows7 x2


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Global And Local Variables in zsh Functions
  2015-08-08 22:44 Global And Local Variables in zsh Functions Georg Wittig
  2015-08-09  0:26 ` ZyX
  2015-08-09  0:31 ` Bart Schaefer
@ 2015-08-09  0:45 ` Mikael Magnusson
  2 siblings, 0 replies; 4+ messages in thread
From: Mikael Magnusson @ 2015-08-09  0:45 UTC (permalink / raw)
  To: Georg Wittig; +Cc: Zsh Users

On Sun, Aug 9, 2015 at 12:44 AM, Georg Wittig <nc-wittigge@netcologne.de> wrote:
> Hi,
>
> I'm writing a zsh script that's constructing a rather complex rsync
> command. One part of the script is constructing  a list of exclude
> commands for rsync. For this I use a global variable EXC. It is built
> in the following way
>
> EXC=''
> function my_f1 () {
>   EXC+=" --exclude='$1'";
> }
> my_f1 /tmp
> my_f1 /opt/windows7
> echo ">>>$EXC<<<"

In addition to the other replies, you'll also want to change EXC to be an array,
EXC=()
my_f1() {
  EXC+=( --exclude=$1 )
}
(note lack of quoting is intentional)

Otherwise, you'll have a bad time trying to actually run the command.

-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-08-09  0:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-08 22:44 Global And Local Variables in zsh Functions Georg Wittig
2015-08-09  0:26 ` ZyX
2015-08-09  0:31 ` Bart Schaefer
2015-08-09  0:45 ` Mikael Magnusson

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).