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

Re: aliases in zsh -c and eval



On Thu, 27 Nov 2014 21:22:22 +0000
Stephane Chazelas <stephane.chazelas@xxxxxxxxx> wrote:
> it looks like aliases defined in `zsh -c` are not expanded in
> there:
> 
> $ zsh -c 'alias foo=echo
> foo bar'
> zsh:2: command not found: foo

Yes, this is normal in text which is input in one go, as documented in
"Aliasing" in the grammar section of the manual, see below.  This is
inevitable as the whole thing is passed to the shell as a single string,
so it's input as a single string.  Presumably some other shells have
input factored in such a way that strings and files are considered more
similarly.


There is a commonly encountered problem with aliases
illustrated by the following code:

example(alias echobar='echo bar'; echobar)

This prints a message that the command tt(echobar) could not be found.
This happens because aliases are expanded when the code is read in;
the entire line is read in one go, so that when tt(echobar) is executed it
is too late to expand the newly defined alias.  This is often
a problem in shell scripts, functions, and code executed with `tt(source)'
or `tt(.)'.  Consequently, use of functions rather than aliases is
recommended in non-interactive code.

pws



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