bash:无法在同一行中设置和使用别名 [英] bash: unable to set and use alias in the same line

查看:115
本文介绍了bash:无法在同一行中设置和使用别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望第二行说的是foo而不是command not found:

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
$

为什么第二行不说foo?经过以下外壳测试,行为相同:

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

  • bash 3.2.5
  • zsh 5.0.8
  • 破折号0.5.9
  • busybox 1.25.0

推荐答案

关于别名的定义和使用的规则有些混乱.在执行该行中的任何命令之前,Bash始终会读取至少一行完整的输入.读取命令时(而不是执行命令时),别名会扩展.因此,与另一命令相同的行上出现的别名定义在读取下一行输入之前不会生效.该行中别名定义之后的命令不受新别名的影响.

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.

这篇关于bash:无法在同一行中设置和使用别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆