I'm trying to figure out how to make the command `false && false` fail when using Bash in what some call 'Strict Mode'. I set up my script with strict error handling using `trap` and `set -Eeuo pipefail`, but I noticed that `false && false` doesn't cause the script to exit. Can anyone explain how to make this happen, or clarify what limitations I might be facing?
2 Answers
Believe it or not, `false && false` does fail, but with `set -e`, the script doesn't exit because it's part of an `&&` list. The first `false` fails, but before the second is evaluated, the shell doesn't exit because of how `&&` works.
According to the man page for `errexit`, it won’t exit the shell if the command failing is right after a `while`, `until`, `if`, or part of an `&&` list. So, the failing command in `false && false` doesn't cause the exit. If you want that first failure to exit, you need to structure your commands differently.
You can use `|| exit` to make the script exit if a command fails. For example:
```bash
true && true || exit # continues
true && false || exit # exits
false && true || exit # exits
false && false || exit # exits
```
Also, it's worth mentioning that the term 'Strict Mode' isn't really an official term in Bash. When you use `set -e`, `set -u`, and `set -o pipefail`, you're enabling specific features—each affecting error handling in different ways.
See the man page for more details on how these work!
Yeah, exactly! Each of those `set` options addresses different error situations, and it’s super important to understand that only the last command in `&&` will always trigger an exit on error.
True! Managing exit conditions can be tricky with Bash scripting. It’s clear that while we call it 'Strict Mode', it doesn’t behave as strictly as it might imply, which can lead to confusion for those not familiar!