Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: is variable with variable name possible?
- X-seq: zsh-users 15929
- From: Mikael Magnusson <mikachu@xxxxxxxxx>
- To: Zsh Users <zsh-users@xxxxxxx>
- Subject: Re: is variable with variable name possible?
- Date: Tue, 5 Apr 2011 00:32:27 +0200
- 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=
- In-reply-to: <20110404205002.GD9371@yahoo.fr>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <Xns9EBD94C46B8C1zzappergmailcom@80.91.229.10> <BANLkTimegXA4_5Co4sSWKgOmY_U-EWE6Pg@mail.gmail.com> <20110404140651.GA9371@yahoo.fr> <BANLkTikffaVdTwNChVZzE6nKh3u9JMMyMA@mail.gmail.com> <20110404162824.GB9371@yahoo.fr> <BANLkTimU=NGp8eMkQKmNony3n4sR8ezreQ@mail.gmail.com> <20110404205002.GD9371@yahoo.fr>
On 4 April 2011 22:50, Stephane Chazelas <stephane_chazelas@xxxxxxxx> 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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author