bash - 参数扩展后 zsh 未被 IFS 拆分

标签 bash shell posix zsh ifs

这是我的代码,用于遍历以冒号分隔的值并执行某些操作 每个值。

f()
{
    IFS=:
    for arg in $1
    do
        echo arg: $arg
    done
}

f foo:bar:baz

这在大多数 POSIX 兼容的 shell 中工作正常。

$ dash foo.sh
arg: foo
arg: bar
arg: baz
$ bash foo.sh
arg: foo
arg: bar
arg: baz
$ ksh foo.sh
arg: foo
arg: bar
arg: baz
$ posh foo.sh
arg: foo
arg: bar
arg: baz
$ yash foo.sh
arg: foo
arg: bar
arg: baz

但它在 zsh 中并没有像预期的那样工作。

$ zsh foo.sh
arg: foo:bar:baz

zsh 是否违反了POSIX在这里?

最佳答案

是的。 Zsh 选择了自己的方式。

这是 zsh 常见问题条目: “3.1: Why does $var where var="foo bar" not do what I expect?”

在这种特殊情况下,您可以通过添加 -y 来解决zsh 调用的选项:

$ zsh -y foo.sh
arg: foo
arg: bar
arg: baz

你可以看看the zsh's faq尤其是第2章和第3章。 您对其他 shell 的体验越多,您就越能发现 zsh 的陷阱。

关于bash - 参数扩展后 zsh 未被 IFS 拆分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46313940/

相关文章:

linux - Shell脚本不从输入文件中获取参数

performance - 防止由于不良工艺导致机器挂起的 shell

Java - 无法创建具有 777 权限的目录(取而代之的是 775)

c - 需要检查管道是否双向工作

c - gethostbyname 双网络接口(interface),选择使用哪一个

python - 为 "Do you want to continue (y/n, default n)"绕过 "setup.py"?

bash - 如何在 Bash 中使用 getopts 允许非可选参数位于可选参数之前?

c - 通过 shell 命令获取 C 应用程序的参数

sql - 逐行比较两个不同文本文件中的两个值

linux - Crontab 无法识别命令