From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 25692 invoked by alias); 8 Aug 2015 22:45:05 -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: 20396 Received: (qmail 1814 invoked from network); 8 Aug 2015 22:45:03 -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=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL autolearn=ham autolearn_force=no version=3.4.0 To: zsh-users@zsh.org From: Georg Wittig Subject: Global And Local Variables in zsh Functions Message-ID: <55C68669.8000408@netcologne.de> Date: Sun, 9 Aug 2015 00:44:57 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit 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]