Why bash's set -e can silently crash scripts over non-error exits
The bash directive set -e causes a script to exit immediately whenever any command returns a non-zero status, which is intended to catch errors. However, many common commands legitimately return non-zero without indicating a real failure, such as grep finding no match, curl receiving a 4xx response, or an arithmetic comparison evaluating to false. This behaviour can cause scripts to terminate unexpectedly during deployments or routine operations, often with no clear error message. Developers can work around the issue by appending '|| true' to suspect commands, toggling set -e off and on around specific lines, or using explicit conditional checks. Experts suggest that while set -e is not inherently bad, it should only be used with a clear understanding of which commands it may inadvertently treat as failures.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in