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

saved from prince of eval



I'm trying to get some mileage out of the '(e)' flag, but it frustrates me:

1:    $ foo="${(e)${array}[${top}, ${bottom}]}"

foo contains the name of the array, nothing more. The doc says that these things can be nested. I tried a few things on one line with no luck. If I insert the literal name of the array in place of "${array}" everything is fine.

But this works:

2:    $ bar='\$${array}[${top}, ${bottom}]'
3:    $ foo="${(e)$(print -R "${(e)${bar}}")}"

I'm not sure how to interpret it tho. Is 'print' doing the work here, or is print a bystander as a nested use of '(e)' works? The expansion of 'bar' with it's single quoted string in '3' has a sort of linearity to it--you'd think that '1' would work, but it refuses to expand all three parameters whereas in '3' the expansion handles all three parameters in a 'dumb' but actually far more intuitive and helpful way--it just does it. I'd like to understand this better, it could be a case of some tiny syntactic error. Why does '1' not work? What's really going on in '3'? In any case, it replaces the evil eval which is very nice:

     $ foo='stranger'
     $ bar='echo howdy $foo'
     $ eval $bar
   howdy stranger                     << Sure ...

     $ eval baz=$bar; echo $baz
   zsh: command not found: howdy << Ferkrissakes
   echo howdy $foo

     $ baz=${(e)bar}; echo $baz << Tranquility
   echo howdy stranger

... so '(e)' is our friend. We should be warned from mother's milk not to use eval, and I have been warned, but, nuts, I didn't realize that there was a safe solution.



Messages sorted by: Reverse Date, Date, Thread, Author