* expansion of nested parameters
@ 1999-11-22 19:02 Phil Pennock
1999-11-22 19:34 ` Zefram
1999-11-22 20:14 ` Bart Schaefer
0 siblings, 2 replies; 3+ messages in thread
From: Phil Pennock @ 1999-11-22 19:02 UTC (permalink / raw)
To: Zsh Users
Hiya, quick question:
Not on list these days, please CC me in on replies - thanks.
This is with zsh-3.0.7:
-----------------------------< cut here >-------------------------------
#!/bin/zsh -f
a=foo
wib_foo_ble=Wow
c=\$wib_${a}_ble
print ${(e)c}
print ${(e)\$wib_${a}_ble}
-----------------------------< cut here >-------------------------------
1: Why does the second print statement print out the current process ID
from $$ instead of printing the same as the first print statement?
2: Is there a way to do this without using an intermediate variable, as
I've done with $c and without using eval statements?
For now, since the job needs doing, I'm using an intermediate, but the
perfectionist in me wants to know what I was doing wrong?
Or is the answer really obvious, and it's my fault for not using zsh so
heavily these days? :^)
Thanks folks,
--
--> Phil Pennock (!PP8185)
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: expansion of nested parameters
1999-11-22 19:02 expansion of nested parameters Phil Pennock
@ 1999-11-22 19:34 ` Zefram
1999-11-22 20:14 ` Bart Schaefer
1 sibling, 0 replies; 3+ messages in thread
From: Zefram @ 1999-11-22 19:34 UTC (permalink / raw)
To: Phil Pennock; +Cc: zsh-users
Phil Pennock wrote:
>a=foo
>wib_foo_ble=Wow
>c=\$wib_${a}_ble
>print ${(e)c}
>1: Why does the second print statement print out the current process ID
> from $$ instead of printing the same as the first print statement?
Because you put "$" where it was expecting a parameter name, and "$"
is in fact a valid parameter name.
>2: Is there a way to do this without using an intermediate variable, as
> I've done with $c and without using eval statements?
Yes. You can use the form "${:-arbitrary string}" to get a $ expansion
that expands to an arbitrary string. $ expansions can be used in
the string. So you want to do
a=foo
wib_foo_ble=Wow
print ${(e):-\$wib_${a}_ble}
This trick is now documented, in zshexpn(1):
# ${name:-word}
# If name is set and is non-null then substitute its
# value; otherwise substitute word. If name is miss-
# ing, substitute word.
-zefram
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: expansion of nested parameters
1999-11-22 19:02 expansion of nested parameters Phil Pennock
1999-11-22 19:34 ` Zefram
@ 1999-11-22 20:14 ` Bart Schaefer
1 sibling, 0 replies; 3+ messages in thread
From: Bart Schaefer @ 1999-11-22 20:14 UTC (permalink / raw)
To: Phil Pennock, Zsh Users
On Nov 22, 7:02pm, Phil Pennock wrote:
} Subject: expansion of nested parameters
}
} This is with zsh-3.0.7:
} -----------------------------< cut here >-------------------------------
} #!/bin/zsh -f
} a=foo
} wib_foo_ble=Wow
} c=\$wib_${a}_ble
} print ${(e)c}
} print ${(e)\$wib_${a}_ble}
} -----------------------------< cut here >-------------------------------
}
} 1: Why does the second print statement print out the current process ID
} from $$ instead of printing the same as the first print statement?
${(e)c} means: Expand ${c}, then apply command substitution etc. to the
result of that expansion.
Hence ${(e)\$wib_${a}_ble} means: Expand ${\$wib_${a}_ble}, then ...
You'll note that ${\$} is the same as ${$} because the command-line parser
strips the backslash before the parameter-name interpreter ever gets around
to looking at it. ${$} is the same as $$; the rest of the "wib_${a}_ble"
stuff is simply ignored once zsh recognizes $$.
} 2: Is there a way to do this without using an intermediate variable, as
} I've done with $c and without using eval statements?
Yeah; it's in the FAQ, question 3.22. What you're asking is a variation:
print ${(e)a:+\$wib_${a}_ble}
should do it.
--
Bart Schaefer Brass Lantern Enterprises
http://www.well.com/user/barts http://www.brasslantern.com
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~1999-11-22 20:15 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-11-22 19:02 expansion of nested parameters Phil Pennock
1999-11-22 19:34 ` Zefram
1999-11-22 20:14 ` Bart Schaefer
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).