下面是我想展示的一个简单测试用例。
在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
,意思是定义一个函数 f,然后调用该函数并传递参数-a
和-l
给ls
命令。 - glenn jackmanargs
变量,而应该使用数组。 - Gilles 'SO- stop being evil'