From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 13467 invoked by alias); 4 Apr 2011 22:32:37 -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: 15929 Received: (qmail 13988 invoked from network); 4 Apr 2011 22:32:35 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received-SPF: pass (ns1.primenet.com.au: SPF record at _spf.google.com designates 209.85.220.171 as permitted sender) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type; bh=Fmtpc8fC2NWH+XqDG02vONFabs+omGIb7h0c4hfm6V8=; b=lMtcVTBqVVsyW1G7bgQmcAicr1evMzF3yqw9S6TnIfjyoqzCRzBdrAvDAz4YiyGQCk yts847X3bMWvGAcxkELZH3+C00mahSGeE+xv+6UDuE39/vtKXLLtsaw2aQqsnqI7PNiY WMJpiC0RaSmbWyfG1ColVTgoyX/qehTTKmf5k= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=PIGU9SdH4Ohn6XJW436dUIji8+WqQg+6DQ/fPR4mVL2fgewdXVBF7JYcxcnvaVezJo QRLkhmuEl12rMFyLbBwXQY3omVXMAqCxoBQ8qo7t2Fo9kyrUjWulLi4m1n7GqaX20s+m Y8NbUZZ0JFjhKTDvqzUcvof8tEQP9PPP/N7Y4= MIME-Version: 1.0 In-Reply-To: <20110404205002.GD9371@yahoo.fr> References: <20110404140651.GA9371@yahoo.fr> <20110404162824.GB9371@yahoo.fr> <20110404205002.GD9371@yahoo.fr> Date: Tue, 5 Apr 2011 00:32:27 +0200 Message-ID: Subject: Re: is variable with variable name possible? From: Mikael Magnusson To: Zsh Users Content-Type: text/plain; charset=UTF-8 On 4 April 2011 22:50, Stephane Chazelas wrote: > 2011-04-04 18:34:26 +0200, Mikael Magnusson: > [...] >> >> > eval "$somevar=\$(pwd)" >> >> > or >> >> > eval "$somevar=\$PWD" > [...] >> >> How do you feel about this? >> >> : ${(P)somevar::=$PWD} >> > [...] >> > >> > Try after >> > >> > sudo ln -s . '/*' >> > cd '/*/*/*/*/*' >> > setopt globsubst >> > >> > ;-) >> > >> > (if you're lucky, it won't crash your machine). >> >> Pretty sure globsubst breaks your idea too, but fine, >> : ${(P)~~somevar::=$PWD} >> In fact, globsubst pretty much breaks every hook function i have :P. > [...] > > eval "$somevar=\$(pwd)" > or > eval "$somevar=\$PWD" > > are POSIX and POSIX shells have globsubst and shwordsplit on. > The above will work regardless of the status of those options. > > In POSIX shells, you need to quote variables to prevent > globsubst or shwordsplit, even in arguments to ":" because > globsubst can be very very resource intensive. > > You can bring a machine to its knees with > > : ${var=foo} > > if $var is for instance /*/*/*/../../../*/*/*/../../../*/*/* > > So, you should either use instead: > > var=${var-foo} > or > : "${var=foo}" > > In your example, if we have to accomodate users enabling > shwordsplit or globsubst (or running zsh in sh or ksh > emulation), we need > > : "${(P)somevar::=$PWD}" > > But > > eval "$somevar=\$PWD" > > is more legible and more portable. But your quotes do nothing (assuming $somevar has a sane value), did you mean eval $somevar=\"\$PWD\" or possibly more readable version eval $somevar='"$PWD"' ? AFAICT, it works without any quotes with globsubst and shwordsplit set too though, as long as you quote the $ in $PWD, tried in both zsh and bash. -- Mikael Magnusson