Zsh 替代形式的复杂命令示例

Tuy*_*ham 2 zsh

请您提供有关 zsh 文档这一部分的见解。我在这个文档上花了很长时间,但机器人可以说清楚:http: //zsh.sourceforge.net/Doc/Release/Shell-Grammar.html#Alternate-Forms-For-Complex-Commands

我正在寻找所有命令的清晰示例if for foreach while until repeat case select function

jim*_*mij 7

这些只是形式更短一些命令,主要是摆脱“多余的”保留字一样thenfidodone,等长格式更便携; 短的只在zsh.


例如长形式 if

if [[ -f file ]] ; then echo "file exists"; else echo "file does not exist"; fi
Run Code Online (Sandbox Code Playgroud)

不仅zsh可以在其他 shell 中工作,也可以在其他 shell 中工作(用单个括号替换双括号以获得更多的便携性)

而短格式

if [[ -f file ]] { echo "file exists" } else { echo "file does not exist" }
if [[ -f file ]] echo file exists
Run Code Online (Sandbox Code Playgroud)

仅适用于zsh.


另一个例子,这次是for循环。

长格式:

for char in a b c; do echo $char; done
for (( x=0; x<3; x++ )) do echo $x; done
Run Code Online (Sandbox Code Playgroud)

短的:

for char in a b c; echo $char
for char (a b c) echo $char             # second version of the same
foreach char (a b c); echo $char; end   # csh-like 'for' loop
for (( x=0; x<3; x++ )) echo $x         # c++ version
for (( x=0; x<3; x++ )) { echo "$x"; }  # also works in bash and ksh
Run Code Online (Sandbox Code Playgroud)

我相信你明白了——我们只是删除了不必要的词,如果列表需要与其他东西分开,用{}. 其余命令:

  • 另请参阅 [Bash for 循环中“do”关键字的用途是什么?](//unix.stackexchange.com/a/306944) 了解不同 Bourne 类 shell 支持的各种形式的 `for` 循环。 (2认同)