bash: unable to set and use alias in the same line

南笙酒味 提交于 2019-11-28 07:57:55

问题


I would expect the second line to say foo instead of command not found:

$ alias foo="echo bac" ; foo;
-bash: foo: command not found
$ foo
bac
$

Why won't the second line say foo? Tested with the following shells, same behavior:

  • bash 3.2.5
  • zsh 5.0.8
  • dash 0.5.9
  • busybox 1.25.0

回答1:


The behaviour you're seeing is described in the Bash Reference Manual (emphasis mine):

The rules concerning the definition and use of aliases are somewhat confusing. Bash always reads at least one complete line of input before executing any of the commands on that line. Aliases are expanded when a command is read, not when it is executed. Therefore, an alias definition appearing on the same line as another command does not take effect until the next line of input is read. The commands following the alias definition on that line are not affected by the new alias.

Presumably the other shells also behave in this way.



来源:https://stackoverflow.com/questions/38526612/bash-unable-to-set-and-use-alias-in-the-same-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!