From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 3013 invoked from network); 25 May 2020 14:23:31 -0000 Received: from ns1.primenet.com.au (HELO primenet.com.au) (203.24.36.2) by inbox.vuxu.org with ESMTPUTF8; 25 May 2020 14:23:31 -0000 Received: (qmail 11645 invoked by alias); 25 May 2020 14:23:22 -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: List-Unsubscribe: X-Seq: 24872 Received: (qmail 16639 invoked by uid 1010); 25 May 2020 14:23:22 -0000 X-Qmail-Scanner-Diagnostics: from rcpt-expgw.biglobe.ne.jp by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.102.3/25821. spamassassin: 3.4.4. Clear:RC:0(133.208.98.3):SA:0(-2.6/5.0):. Processed in 2.412712 secs); 25 May 2020 14:23:22 -0000 X-Envelope-From: takimoto-j@kba.biglobe.ne.jp X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | Received-SPF: pass (ns1.primenet.com.au: SPF record at spf01.biglobe.ne.jp designates 133.208.98.3 as permitted sender) X-Biglobe-Sender: Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.14\)) Subject: Re: Avoid duplication of code From: "Jun. T" In-Reply-To: <20200525.180259.1237420001357827351.yasu@utahime.org> Date: Mon, 25 May 2020 23:22:39 +0900 Cc: Yasuhiro KIMURA Content-Transfer-Encoding: 7bit Message-Id: References: <20200525.180259.1237420001357827351.yasu@utahime.org> To: zsh-users@zsh.org X-Mailer: Apple Mail (2.3445.104.14) X-Biglobe-Spnum: 11943 > 2020/05/25 18:02, Yasuhiro KIMURA wrote: > > 1. Define function that updates single environment variable and pass > variable name as argument of it. > 2. Use 'for name in word; do list done' such as following. > > for valname in SSH_AUTH_SOCK SSH_AGENT_PID > do > (update value of environment variable named ${valname}) > done > > But it seems that both require nested variable expansion and that it > is impossible. Then is there any other way to avoid code duplication? Suppose a parameter, say valname, contains a name of another parameter; valname=SSH_AUTH_SOCK Then you can GET the current value of SSH_AUTH_SOCK by if [[ -n ${(P)valname} ]]; then ... You can SET a new value to SSH_AUTH_SOCK in a couple of ways. In your case, probably export $valname='new value' would be the simplest. If you don't want to export the variable, then, : ${(P)valname::=new value}