以下是我想说明的一个简单测试用例。变量扩展在zsh中与bash中的变量不同
在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
但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
在上面zsh的最后一行,给了我下面的错误
ls: invalid option -- ' '
Try `ls --help' for more information.
我想的zsh正在执行
ls "-a -l"
这是我得到同样的错误。
那么,我该如何得到bash的行为呢?
我不确定我是否清楚,请告诉我是否有您想要知道的事情。
这不回答你的问题,但我认为这是更好的:'F(){LS “$ @”; }; f -a -l' –
@glenn,是的,绝对是。正如我所说的,这只是一个更大的问题的例子,我必须使用args变量。 –
@ShrikantSharat如果您正在为bash和zsh(和ksh)编写脚本,请不要使用'args'变量来执行此操作。改用数组。 – Gilles