From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 15521 invoked by alias); 9 Aug 2015 00:32:54 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 20399 Received: (qmail 9010 invoked from network); 9 Aug 2015 00:32:51 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,FSL_HELO_BARE_IP_2,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL autolearn=no autolearn_force=no version=3.4.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail; t=1439079994; bh=xOs5b3czCZfUBDWZ4+jTbaDSi5irspH8EhYbf9hLh9g=; h=From:To:In-Reply-To:References:Subject:Date; b=nOzSGMAelGDd8gdSTluMrxe6lPAH5YPqy4Kdi8Yh+/T0HWHmjM0D4CNst89w3Aymy HTr6Mxx3cE4c8ELpVAQvcNaJDJSgCw63HBN1oB3Mr2GNueDi7un4p/uTCgUevp/rN6 6Qo8rHxpV3OhOr79FlaqPt3f1Bpf4VtWgj3yH/VU= From: ZyX To: Georg Wittig , "zsh-users@zsh.org" In-Reply-To: <55C68669.8000408@netcologne.de> References: <55C68669.8000408@netcologne.de> Subject: Re: Global And Local Variables in zsh Functions MIME-Version: 1.0 Message-Id: <2120121439079993@web6g.yandex.ru> X-Mailer: Yamail [ http://yandex.ru ] 5.0 Date: Sun, 09 Aug 2015 03:26:33 +0300 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=koi8-r 09.08.2015, 01:46, "Georg Wittig" : > 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]