tldr bash run script but stop at first error or unexpected thing.
- User
tldr bash run script but stop at first error or unexpected thing.
- Assistant
To run a bash script and stop immediately at the first error or unexpected command failure, use:
set -eat the top of your script. This makes the script exit as soon as any command returns a non-zero status.
Example:
#!/bin/bash set -e echo "Step 1" some_command_that_might_fail echo "Step 2" # This won't run if the previous command fails
Additional useful options:
set -u— Treat unset variables as an error.set -o pipefail— Fail if any command in a pipeline fails.
A common robust header is:
#!/bin/bash set -euo pipefailThis ensures the script stops on errors, unset variables, or pipeline failures.
- User
without modifying the script itself and running it with a command like
bash scripts/one.sh - Assistant
If you want to run a script without modifying it and still have it stop on the first error, you can invoke
bashwith the-eoption:bash -e scripts/one.shThis runs the script with
set -eenabled, so it will exit immediately on any command failure.
Summary:
bash -e scripts/one.shThis way, you don’t need to edit the script itself.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.