Skip to content
The Chat Archive
gpt-4.1-mini-2025-04-14· OpenAI LMArena Human Preference 140K

tldr bash run script but stop at first error or unexpected thing.

  1. User

    tldr bash run script but stop at first error or unexpected thing.

  2. Assistant

    To run a bash script and stop immediately at the first error or unexpected command failure, use:

    set -e
    

    at 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 pipefail
    

    This ensures the script stops on errors, unset variables, or pipeline failures.

  3. User

    without modifying the script itself and running it with a command like bash scripts/one.sh

  4. Assistant

    If you want to run a script without modifying it and still have it stop on the first error, you can invoke bash with the -e option:

    bash -e scripts/one.sh
    

    This runs the script with set -e enabled, so it will exit immediately on any command failure.


    Summary:

    bash -e scripts/one.sh
    

    This way, you don’t need to edit the script itself.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.