Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: is variable with variable name possible?



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