无法理解 Fish shell 中的命令替换
在sh中:
~$ `echo ls`
bin/ Desktop/
但是在fish中:(
fish: Illegal command name “(echo ls)”
~% (echo ls)
请注意,错误消息出现在命令行上方。)
~% echo (echo ls)
ls
~% eval (echo ls)
bin/ Desktop/
fish: Illegal command name “(echo ls)”
exec (echo ls)
^
~% exec (echo ls)
命令替换似乎只能作为命令的参数,而不是作为命令本身?为什么?
嗯,帮助文档确实说
如果参数包含一组括号,则括号内的文本将被解释为命令列表。
但还是,为什么?
In sh:
~$ `echo ls`
bin/ Desktop/
But in fish:
fish: Illegal command name “(echo ls)”
~% (echo ls)
(Note that the error message appears above the command line.)
~% echo (echo ls)
ls
~% eval (echo ls)
bin/ Desktop/
fish: Illegal command name “(echo ls)”
exec (echo ls)
^
~% exec (echo ls)
It seems that command substitution only works as parameters of a command, not as a command itself? Why?
Well, the help doc does say
If a parameter contains a set of parenthesis, the text enclosed by the parenthesis will be interpreted as a list of commands.
But still, why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新
这个答案是十年前的2010年写的。
Fish 的最新版本(我在 3.1.2 上进行了测试)已更新并设置 cmd ls; $cmd 现在有效。
这是
因为命令替换属于参数扩展,不允许作为命令。
类似的例子:
在sh中:
但在fish中:
为什么
简而言之,这有利于可验证性
文章 解释了详细信息:
出于同样的原因,不允许将命令替换作为命令。
(注意:引用的例子是不公平的,因为“if”和“fi”不是简单的命令而是保留字。请参阅下面的评论。)
Update
This answer was written ten year ago in 2010.
Recent versions of fish (I tested on 3.1.2) updated and
set cmd ls; $cmd
is valid now.How
This because command substitutions belong to parameter expansions and are not allowed as commands.
A similar example:
in sh:
But in fish:
Why
In short, it's good for verifiability
This article explains details:
For the same reason, command substitutions are not allowed as commands.
(Note: The cited example is not fair, since 'if' and 'fi' are not simple commands but reserved words. See comments below.)
它与扩展的顺序有关。
来自
fish
中的help Expand-command-substitution
:来自
man bash
:It has to do with the order of expansions.
From
help expand-command-substitution
infish
:From
man bash
: