Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
有没有办法让我的 bash 脚本在第一个返回非零状态的命令上终止?
我知道我可以用&&' 链接它:
&&
cd /stuff && echo 'what's up' && ....
还有其他方法吗?
是的,这就像在shebang之后的脚本开头添加一样简单:
set -e
如果需要(仅部分代码),您可以停止此操作
set +e
或在shebang上:
#!/bin/bash -e
或通过调用脚本:
bash -e script.bash