Variable expansion inside of functions
Klaus Ethgen <[email protected]> Sat, 18 Apr 2026 13:33:30 +0100
| Newsgroups | gmane.comp.shells.zsh.user |
|---|---|
| Message-ID | <aeN6GjRBGVWDG6SP@chil> |
Hi,
I have a strange problem. I need to change a command to a function.
Theoretically that works like a charm. But now I want to make that
definition a bit more general and struggle as variables gets not
evaluated when it should be.
Here is my definition:
```
for i in pdfgrep rg
do
if whence -p "$i" >&-
then
"$i"()
{
local -a arg
local val
for val in "$@"
do
if [[ -a "$val" ]]
then
arg+="$val"
else
arg+="$(print -- "$val" | iconv -f $(locale charmap) -t UTF-8)"
fi
done
command "$i" "$arg[@]"
}
fi
done
```
When I do that, I get the following for pdfgrep:
```
~> which pdfgrep
pdfgrep () {
local -a arg
local val
for val in "$@"
do
if [[ -a "$val" ]]
then
arg+="$val"
else
arg+="$(print -- "$val" | iconv -f $(locale charmap) -t UTF-8)"
fi
done
command "$i" "$arg[@]"
}
```
As you can see, $i is not evaluated in the function definition.
Is there any way to evaluate $i inside the function definition; but only
that variable?
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 iQHPBAABCgA5FiEE2oNkebTd5YKFiBG5fue2cNoeGfgFAmnjefwbFIAAAAAABAAO bWFudTIsMi41KzEuMTIsMiwyAAoJEH7ntnDaHhn49TsMAIy85vLLZud9RAcFNMvo wuVtdW7HG7fYVk4R2ysYKl61VmOgipBln4SpbMTjOW7cjV+dLqM2OssKclzOP2R6 Kl5q8iuadMszoAQwEM95OILG9wxsgFG+N/WUps612g2Rodymzp8tqM0Ns4ZkhDS0 J1gO2L184GC+RyutB1GQntlWdLSTShGXZUdtMmUq0lO/KDYhxSHG6LZTAgaWB2Qs 3nXfLoIjKZZmLd4nelYMRvUYIGIf8NaewMHOD0GEpR29rIDvArlX41BI8Gj0fk61 xdpVEtOIOpaRvqMOkSjnJiPpyL8dI7GXhYsKU6yu3M4MstjptnAUb4i/Uj7BW6QR xKA4PKdK4ZbC0TNLxuZHMWF1CiUWHgxHRMmuHvlUEiUxwfYTis22mvN2wLaivCZZ qb9+zHNBzDslB4QdshwMJxtPnHc3rJgUQZd+vZxc9ZhokdVy62/LTss61gmltDJ9 fuvPVwjLXSGvoi5+fI85DS8PT5OnfGxxm31cKQ3wztgmcw== =ECfO -----END PGP SIGNATURE-----