Alw*_*win 6 shell bash zsh shell-script
我有一个简单的脚本,涉及for
bash 中的循环,我试图在 zsh 中使用它。我曾假设 shebang 将确保使用符合 POSIX 标准的 shell(在我的系统上/bin/sh -> dash*
),因此不会出现任何问题。
MWE 脚本ITEMS
实际上是列出软件包的命令的输出,例如ITEMS=$(pip freeze)
:
#!/bin/sh
# ITEMS=$(pip freeze) # Example of useful command
ITEMS="Item1
Item2
Item3" # Dummy variable for testing
for ITEM in $ITEMS; do
echo $ITEM
echo Complete
done
Run Code Online (Sandbox Code Playgroud)
这是我尝试在以下位置运行脚本时的输出zsh
:
$ source scratch.sh
Item1
Item2
Item3
Complete # Undesired
$ . ./scratch.sh
Item1
Item2
Item3
Complete # Undesired
$ bash scratch.sh
Item1
Complete
Item2
Complete
Item3
Complete # Desired
$ sh scratch.sh
Item1
Complete
Item2
Complete
Item3
Complete # Desired
Run Code Online (Sandbox Code Playgroud)
当我在 bash 终端中运行它时,它工作正常。我想我误解了 shebang 是如何解释的zsh
?有人可以向我解释一下应该如何使用它,以便当我运行时source scratch.sh
或者. ./scratch.sh
我有与运行时相同的输出吗sh scratch.sh
?zsh
我知道我可以修改我的 for 循环脚本以使其与本机兼容bash
,但我想使用/bin/sh -> dash
,因此我始终使用 posix 兼容的 shell,而不必担心 bashisms 或 zshisms。
抱歉,如果这是一个基本问题,我确实搜索了zsh
,posix
和 shebang 但没有找到类似的问题。
仅当您直接执行脚本而不指定如何运行它时, shebang 才会产生影响;也就是说,使用类似./scratch.sh
或/path/to/scratch.sh
的东西将其放入您的目录中PATH
并仅使用scratch.sh
.
如果您使用其他命令运行它,它将控制它所做的事情(覆盖 shebang)。如果你使用bash scratch.sh
,它会运行在bash
;如果你使用zsh scratch.sh
,它会运行在zsh
;如果您使用sh
,它可以在sh
您的系统上运行(dash
在您的具体情况下)。
如果您使用source scratch.sh
or . scratch.sh
,它会在当前 shell中运行,无论是什么。.
这就是和命令的全部目的source
。再次强调,这里的 shebang 被忽略了。