不能理解鱼壳中的命令替换 [英] Cannot understand command substitution in Fish shell

查看:186
本文介绍了不能理解鱼壳中的命令替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在sh:

~$ `echo ls`
bin/  Desktop/

但在鱼中:

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?

好的,帮助文档说


一组括号,括号括起来的文本将被解释为命令列表。

If a parameter contains a set of parenthesis, the text enclosed by the parenthesis will be interpreted as a list of commands.

但是,为什么?

推荐答案

如何



这是因为命令替换属于参数扩展,不允许作为命令。

How

This because command substitutions belong to parameter expansions and are not allowed as commands.

类似示例:

在sh中:

tmpls=ls
$tmpls

但在鱼:

% set cmd ls; $cmd
fish: Variables may not be used as commands.
...



为什么



<

Why

In short, it's good for verifiability

article 说明详情:


由于允许使用变量作为常规shell中的命令,不可能可靠地检查脚本的语法。例如,这个bash / zsh代码段可能是合法的,也可能不合法,取决于你的运气。你感觉幸运吗?

Since it is allowed to use variables as commands in regular shells, it is impossible to reliably check the syntax of a script. For example, this snippet of bash/zsh code may or may not be legal, depending on your luck. Do you feel lucky?

    if true; then if [ $RANDOM -lt 1024 ]; then END=fi; else END=true; fi; $END

bash和zsh都尝试确定当前缓冲区中的命令是否完成按回车键,但由于这样的问题,他们有时会失败。更糟糕的是,这条完美的法律代码被bash拒绝:

Both bash and zsh try to determine if the command in the current buffer is finished when the user presses the return key, but because of issues like this, they will sometimes fail. Even worse, this piece of perfectly legal code is rejected by bash:

  FI=fi; foo() { if true; then true; $FI; }

Fish避免了这种问题,因为变量不允许作为命令。使用eval命令或使用函数,可以使用变量作为命令执行任何操作。

Fish avoids this kind of problem, since variables are not allowed as commands. Anything you can do with variables as commands can be done in a much cleaner way using either the eval command or by using functions.

同样的原因,命令替换不允许作为命令。

For the same reason, command substitutions are not allowed as commands.

(注意:引用的示例不公平,因为'if'和'fi'不是简单的命令,保留字。见下面的注释。)

(Note: The cited example is not fair, since 'if' and 'fi' are not simple commands but reserved words. See comments below.)

这篇关于不能理解鱼壳中的命令替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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