Re: Variable expansion inside of functions

Klaus Ethgen <[email protected]> Sat, 18 Apr 2026 15:26:30 +0100
Newsgroups gmane.comp.shells.zsh.devel,gmane.comp.shells.zsh.user
Message-ID <aeOUljwEFHiGKZ5j@chil>
Hi

Am Sa den 18. Apr 2026 um 14:53 schrieb Aaron Schrab:
> >Is there any way to evaluate $i inside the function definition; but 
> >only that variable?
> 
> As far as I know, there isn't a way to do that.

I tried with an eval but that is horrible.

> Are you trying to do this only in zsh, or are you looking for something 
> that would work in bash as well? For just zsh, unless you've unset the 
> FUNCTION_ARGZERO option you should be able to use "$0" for that inside 
> of the function definition.

That is an idea. Works that way.

> Also, unless you've unset the MULTI_FUNC_DEF option you could also 
> eliminate the loop if not for the checking if the command exists. It 
> might be clearer to have the loop just build up a $cmds array (I'd call 
> it $commands, but zsh already defines that) of the ones that you want to 
> wrap and then define the functions outside the loop. I think which way 
> to do it is a question of taste.

I didn't know I can do it that way.

> And, with the default options (if SH_WORD_SPLIT isn't set) you can just 
> use $arg unquoted when calling the original command. You don't need the 
> quotes around $0 (or $i) either. Although I do tend to still quote 
> things to keep in the habit, since I often need to write scripts for 
> bash or even sh.

Yea, same here. I overly quote stuff, even if not needed.

>      (( $+commands[$i] )) && cmds+=$i

Sane solution for whence -p. I did not know $+... And I do not find it
in the documentation. Only ${{+...}} but that is about parameters and
not about arrays.

Regards
   Klaus
-- 
Klaus Ethgen                                       http://www.ethgen.ch/
pub  4096R/4E20AF1C 2011-05-16            Klaus Ethgen <[email protected]>
Fingerprint: 85D4 CA42 952C 949B 1753  62B3 79D0 B06F 4E20 AF1C
signature.asc (application/pgp-signature, 728 B)
-----BEGIN PGP SIGNATURE-----
Comment: Charset: ISO-8859-1

iQHPBAABCgA5FiEE2oNkebTd5YKFiBG5fue2cNoeGfgFAmnjlIMbFIAAAAAABAAO
bWFudTIsMi41KzEuMTIsMiwyAAoJEH7ntnDaHhn4StgMAJyVQB3jTx/WNnpyoMZG
uncud1Qn/pKdKzhSCVgHPeA/g69OAiCka1gogkjKI2FgthLq9vhESQ353hsfZAVQ
mH9A2wXLEu8Mv+zTFJ+m8q9UbZZ5Bcw+gnu8hhmUHMubINzR2j5YGUaNvYJGZObj
usXh3+lYqkTJiy4mbOiKSeEl+3yTqvLvSC5WOCGfga0Do9MqeK9ZeBto6WmDgTG+
ObkTcGB3tzTrjCwPoVnWZBAq+nUj+QDk0EcbiPSBMgJoKHfjIhbVVFm3KuxWcN67
fQouzpBTYyr9sIIlsAA0EPtSFCgSulLmNpLUlXvaG0I7k5RwAbs5qEtCahEQEpDR
xv/GUl52/SR8TNvvtgGb+E5Tsfql2EIJjT/hpMx4DY0Cau7vdQv7oBu+SpFCfL4z
dm0bQfUlPsJP1qd5pfYeAgQ6sl2kVDEWIKq/MJN6M3TgDiQSTWSvrzSpv1Xq5Anr
KfIpMx9DpBOtw5S/kWpJvjqtQykj40qtKQs2mmBA+367hQ==
=Jh47
-----END PGP SIGNATURE-----