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

Re: expansion of nested parameters

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

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