From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 24745 invoked by alias); 5 Apr 2011 04:10:25 -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: 15932 Received: (qmail 23502 invoked from network); 5 Apr 2011 04:10:23 -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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham version=3.3.1 Received-SPF: pass (ns1.primenet.com.au: SPF record at np.by designates 91.149.189.205 as permitted sender) From: ZyX Reply-To: zyx@np.by To: Mikael Magnusson , Zsh Users Subject: Re: is variable with variable name possible? Date: Tue, 5 Apr 2011 08:10:19 +0400 User-Agent: KMail/1.13.5 (Linux/2.6.36-zen1; KDE/4.5.5; x86_64; ; ) References: <20110404205002.GD9371@yahoo.fr> In-Reply-To: <20110404205002.GD9371@yahoo.fr> MIME-Version: 1.0 Message-Id: <201104050810.19865@-zyx> Content-Type: multipart/signed; boundary="nextPart2395511.ZcuxSn82A7"; protocol="application/pgp-signature"; micalg=pgp-sha1 Content-Transfer-Encoding: 7bit --nextPart2395511.ZcuxSn82A7 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Reply to message =ABRe: is variable with variable name possible?=BB,=20 sent 00:50:02 05 April 2011, Tuesday by Stephane Chazelas: > In your example, if we have to accomodate users enabling > shwordsplit or globsubst (or running zsh in sh or ksh > emulation), we need >=20 > : "${(P)somevar::=3D$PWD}" If we are talking about script, then you should use `emulate -L zsh' somewh= ere=20 at the top. This will deal with all possible weird options that are set by = user. > eval "$somevar=3D\$(pwd)" > or > eval "$somevar=3D\$PWD" >=20 > are POSIX and POSIX shells have globsubst and shwordsplit on. > The above will work regardless of the status of those options. Somebody said that $somevar contains nothing harmful? Original message: > 2011-04-04 18:34:26 +0200, Mikael Magnusson: > [...] >=20 > > >> > eval "$somevar=3D\$(pwd)" > > >> > or > > >> > eval "$somevar=3D\$PWD" >=20 > [...] >=20 > > >> How do you feel about this? > > >>=20 > > >> : ${(P)somevar::=3D$PWD} > > >=20 > > > [...] > > >=20 > > > Try after > > >=20 > > > sudo ln -s . '/*' > > > cd '/*/*/*/*/*' > > > setopt globsubst > > >=20 > > > ;-) > > >=20 > > > (if you're lucky, it won't crash your machine). > >=20 > > Pretty sure globsubst breaks your idea too, but fine, > >=20 > > : ${(P)~~somevar::=3D$PWD} > >=20 > > In fact, globsubst pretty much breaks every hook function i have :P. >=20 > [...] >=20 > eval "$somevar=3D\$(pwd)" > or > eval "$somevar=3D\$PWD" >=20 > are POSIX and POSIX shells have globsubst and shwordsplit on. > The above will work regardless of the status of those options. >=20 > 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. >=20 > You can bring a machine to its knees with >=20 > : ${var=3Dfoo} >=20 > if $var is for instance /*/*/*/../../../*/*/*/../../../*/*/* >=20 > So, you should either use instead: >=20 > var=3D${var-foo} > or >=20 > : "${var=3Dfoo}" >=20 > In your example, if we have to accomodate users enabling > shwordsplit or globsubst (or running zsh in sh or ksh > emulation), we need >=20 > : "${(P)somevar::=3D$PWD}" >=20 > But >=20 > eval "$somevar=3D\$PWD" >=20 > is more legible and more portable. --nextPart2395511.ZcuxSn82A7 Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part. -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.17 (GNU/Linux) iEYEABECAAYFAk2alisACgkQTFU0h8DxG4Nc/QCeJm7L5+0AEI2uy505I4oRpoQi gLcAnRHM8xSmxH+R8eWCEHXdsA3d58i0 =FjWJ -----END PGP SIGNATURE----- --nextPart2395511.ZcuxSn82A7--