bash脚本中的“什么都不做”行


15

我有一个bash脚本,并且有一个else声明,我希望它什么也不做。

做这个的最好方式是什么?

Answers:


32

标准方法是使用冒号:

if condition; do
  command
else
  :
fi

true

if condition; do
  command
else
  true
fi

但是为什么不跳过这一else部分:

if condition; do
  command
fi

zsh和中yash,您甚至可以将else零件设为空:

if condition; then
  command
else
fi

6

else语句是可选的。只需输入:

if [ expr ]; then
#bla
#bla
#bla
fi
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.