我有以下bash脚本。我设置了errexit(set -e),我希望在错误的cd
命令之后退出该函数。
#!/bin/bash
set -e
wtf() {
cd non_existent_directory
echo "SHOULD EXIT BEFORE THIS PRINTS"
}
wtf || echo "WORKED AS EXEPECTED"
但遗憾的是它没有:
$ ./wtf.sh
./wtf.sh: line 6: cd: non_existent_directory: No such file or directory
SHOULD EXIT BEFORE THIS PRINTS
如何让它在回声之前退出?我在这里查看了-e
选项:http://www.tldp.org/LDP/abs/html/options.html
并且不明白为什么这不起作用?