可变扩张是zsh的不同,在庆典 [英] Variable expansion is different in zsh from that in bash

查看:116
本文介绍了可变扩张是zsh的不同,在庆典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个简单的测试案例是我想说明

在bash中,

 #定义函数f
F(){LS的$ args; }#运行命令`ls`
F#运行fommand`LS -a`
ARGS = - 一个
F#运行命令`LS -a -l`
ARGS = - 一个-l
F

但在zsh中

 #定义函数f
F(){LS的$ args}#运行命令`ls`
F#运行fommand`LS -a`
ARGS = - 一个
F#我希望它运行`LS -a -l`,相反,它给我一个错误
ARGS = - 一个-l
F

在上面的zsh的最后一行,给我下面的错误

  LS:无效选项 - 
尝试`LS --help'以获取更多信息。

我想的zsh正在执行

  LS-a -l

这是当我得到同样的错误。

所以,我怎么在这里得到的bash的行为?

我不知道如果我不清楚,让我知道,如果有你想知道的东西。


解决方案

不同的是,(默认情况下)的的zsh 的没有做不带引号的参数扩展分词。

您可以启用通过设置SH_WORD_SPLIT选项或使用 = 标志对个人扩张正常二字拆分:

  LS $ {= ARGS}

  SETOPT SH_WORD_SPLIT
LS的$ args

如果你的目标的炮弹支持阵列( KSH 庆典的zsh 的),那么你可能会更好使用数组:

  ARGS =( - 一-l)
LS$ {ARGS [@]}


的zsh 的常见问题解答

的zsh 的手动


  • 14.3参数扩展


      

    请特别注意一个事实,即不带引号的参数的话是不会自动空白,除非选项SH_WORD_SPLIT设置分割;看到这个选项下面的参考更多详细信息。这是其他shell的一个重要区别。



  • SH_WORD_SPLIT


      

    会导致在不带引号的参数扩展进行场分裂。



The following is a simple test case for what I want to illustrate.

In bash,

# define the function f
f () { ls $args; }

# Runs the command `ls`
f

# Runs the fommand `ls -a`
args="-a"
f

# Runs the command `ls -a -l`
args="-a -l"
f

But in zsh

# define the function f
f () { ls $args }

# Runs the command `ls`
f

# Runs the fommand `ls -a`
args="-a"
f

# I expect it to run `ls -a -l`, instead it gives me an error
args="-a -l"
f

The last line in the zsh on above, gives me the following error

ls: invalid option -- ' '
Try `ls --help' for more information.

I think zsh is executing

ls "-a -l"

which is when I get the same error.

So, how do I get bash's behavior here?

I'm not sure if I'm clear, let me know if there is something you want to know.

解决方案

The difference is that (by default) zsh does not do word splitting for unquoted parameter expansions.

You can enable "normal" word splitting by setting the SH_WORD_SPLIT option or by using the = flag on an individual expansion:

ls ${=args}

or

setopt SH_WORD_SPLIT
ls $args

If your target shells support arrays (ksh, bash, zsh), then you may be better off using an array:

args=(-a -l)
ls "${args[@]}"


From the zsh FAQ:

From the zsh Manual:

  • 14.3 Parameter Expansion

    Note in particular the fact that words of unquoted parameters are not automatically split on whitespace unless the option SH_WORD_SPLIT is set; see references to this option below for more details. This is an important difference from other shells.

  • SH_WORD_SPLIT

    Causes field splitting to be performed on unquoted parameter expansions.

这篇关于可变扩张是zsh的不同,在庆典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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