加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 无法理解Fish shell中的命令替换

发布时间:2020-12-15 17:00:44 所属栏目:安全 来源:网络整理
导读:在sh: ~$`echo ls`bin/ Desktop/ 但在鱼类: 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) ^~%
在sh:
~$`echo ls`
bin/  Desktop/

但在鱼类:

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)

看来命令替换只能作为命令的参数,而不是命令本身?为什么?

好吧,帮助文档确实说

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

但是,为什么呢?

怎么样

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

一个类似的例子:

在sh:

tmpls=ls
$tmpls

但在鱼类:

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

为什么

简而言之,它对可验证性有好处

这个article解释了细节:

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?

06002

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:

06003

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.

出于同样的原因,命令替换不允许作为命令.

(注意:引用的例子不公平,因为’if’和’fi’不是简单的命令而是保留字.请参阅下面的评论.)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读