zsh-workers
 help / color / mirror / code / Atom feed
* aliases in zsh -c and eval
@ 2014-11-27 21:22 Stephane Chazelas
  2014-11-27 21:25 ` Stephane Chazelas
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Stephane Chazelas @ 2014-11-27 21:22 UTC (permalink / raw)
  To: Zsh hackers list

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: aliases in zsh -c and eval
  2014-11-27 21:22 aliases in zsh -c and eval Stephane Chazelas
@ 2014-11-27 21:25 ` Stephane Chazelas
  2014-11-27 21:35 ` Mikael Magnusson
  2014-11-27 21:35 ` Peter Stephenson
  2 siblings, 0 replies; 4+ messages in thread
From: Stephane Chazelas @ 2014-11-27 21:25 UTC (permalink / raw)
  To: Zsh hackers list

2014-11-27 21:22:22 +0000, Stephane Chazelas:
> 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.
[...]

For completeness:

$ cat > a
alias foo=echo
foo bar
$ zsh a
bar
$ zsh -c '. ./a'
bar
$ zsh -c 'eval "$(cat ./a)"'
(eval):2: command not found: foo
$ ksh a
bar
$ ksh -c '. ./a'
ksh: .[2]: foo: not found [No such file or directory]

ksh worse this time.

-- 
Stephane


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: aliases in zsh -c and eval
  2014-11-27 21:22 aliases in zsh -c and eval Stephane Chazelas
  2014-11-27 21:25 ` Stephane Chazelas
@ 2014-11-27 21:35 ` Mikael Magnusson
  2014-11-27 21:35 ` Peter Stephenson
  2 siblings, 0 replies; 4+ messages in thread
From: Mikael Magnusson @ 2014-11-27 21:35 UTC (permalink / raw)
  To: Zsh hackers list

On Thu, Nov 27, 2014 at 10:22 PM, Stephane Chazelas
<stephane.chazelas@gmail.com> 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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: aliases in zsh -c and eval
  2014-11-27 21:22 aliases in zsh -c and eval Stephane Chazelas
  2014-11-27 21:25 ` Stephane Chazelas
  2014-11-27 21:35 ` Mikael Magnusson
@ 2014-11-27 21:35 ` Peter Stephenson
  2 siblings, 0 replies; 4+ messages in thread
From: Peter Stephenson @ 2014-11-27 21:35 UTC (permalink / raw)
  To: Zsh hackers list

On Thu, 27 Nov 2014 21:22:22 +0000
Stephane Chazelas <stephane.chazelas@gmail.com> 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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2014-11-27 21:35 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-11-27 21:22 aliases in zsh -c and eval Stephane Chazelas
2014-11-27 21:25 ` Stephane Chazelas
2014-11-27 21:35 ` Mikael Magnusson
2014-11-27 21:35 ` Peter Stephenson

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).