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

Re: aliases in zsh -c and eval



On Thu, Nov 27, 2014 at 10:22 PM, Stephane Chazelas
<stephane.chazelas@xxxxxxxxx> wrote:
> Hiya,
>
> 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
>
> Those defined for instance in ~/.zshenv are expanded. And:
>
> $ zsh -c 'alias foo=echo
> eval foo bar'
> bar
>
> It looks like zsh is the only one out here. (with bash you need
> bash -O expand_aliases).
>
> Same with eval:
>
> $ (eval 'alias foo=echo
> subsh quote> foo bar')
> zsh: command not found: foo
> $ (eval 'alias foo=echo
> eval foo bar')
> bar
>
> This time, ksh93 does behave like zsh.
>
> --
> Stephane

This is documented near the start of the "Aliasing" section of the manpage.

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

              alias echobar='echo bar'; echobar

       This prints a message that the command 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 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 `source' or `.'.  Consequently, use of functions rather than
aliases is recommended in non-interactive code.

-- 
Mikael Magnusson



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