这是一个能够复现问题的示例脚本:
< p >< em > example.zsh < /em >< /p >
运行脚本的当前结果:
zmodules=(foo bar/baz)
local zmodule
for zmodule (${zmodules}); do
local zurl zname
zname=${zmodule##*/}
case ${#zmodule//[^\/]/} in
0) zurl="https://github.com/foo/${zmodule}" ;;
1) zurl="https://github.com/${zmodule}" ;;
esac
print "${zurl} ${zname}"
done
运行脚本的当前结果:
$ source ./example.zsh 2>/dev/null
zmodule=bar/baz
zurl=https://github.com/bar/baz
zname=baz
https://github.com/foo/foo foo
zurl=https://github.com/foo/foo
zname=foo
https://github.com/bar/baz baz
预期结果:
$ source ./example.zsh 2>/dev/null
https://github.com/foo/foo foo
https://github.com/bar/baz baz
这里我漏掉了什么?
编辑: 我想我明白了: local
只在函数内部起作用。但是当我声明变量为局部变量时,为什么它仍然会打印赋值呢?