可变扩张是zsh的不同,在庆典 [英] Variable expansion is different in zsh from that in bash
问题描述
下面是一个简单的测试案例是我想说明
在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 [@]}
-
经典不同的是分词,有关 3.1 讨论;这捕获了很多的zsh开始用户。
块引用> -
3.1:为什么是$ var其中var =富巴没有做什么,我期待是覆盖这一问题的FAQ。
-
请特别注意一个事实,即不带引号的参数的话是不会自动空白,除非选项SH_WORD_SPLIT设置分割;看到这个选项下面的参考更多详细信息。这是其他shell的一个重要区别。
块引用> -
会导致在不带引号的参数扩展进行场分裂。
块引用>
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:
2.1: Differences from sh and ksh
The classic difference is word splitting, discussed in question 3.1; this catches out very many beginning zsh users.
3.1: Why does $var where var="foo bar" not do what I expect? is the FAQ that covers this question.
From the zsh Manual:
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.
Causes field splitting to be performed on unquoted parameter expansions.
这篇关于可变扩张是zsh的不同,在庆典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!