Urgent.News

What's breaking now, across thousands of outlets.

AI

The AI-written shell script that ran from the wrong folder and exited 0: `set -euo pipefail`, and where it still won't save you

Result first: a four-line script whose cd failed (because BUILD_DIR was unset) kept going. It printed the wrong working directory, ran its "deploy" line, and exited 0. Adding one line, set -euo pipefail , made it stop on the first error with exit code 1: BUILD_DIR: unbound variable . #!/usr/bin/env bash set -euo pipefail cd " $BUILD_DIR /out" What each flag does (bash's own help set ): -e : "Exit…

A four-line shell script failed due to an unset BUILD_DIR variable, continuing execution and printing the incorrect working directory before running the deploy command and exiting with a status of 0. The addition of set -euo pipefail caused the script to halt immediately upon encountering the first error, displaying the error message "BUILD_DIR: unbound variable". Here is a breakdown of each flag:

-e: The script exits immediately if any command returns a non-zero exit status.

-u: The shell treats unset variables as an error during substitution.

-o pipefail: The return value of a pipeline is determined by the last command that exits with a non-zero status, or zero if no command fails.

However, even with set -e in place, it does not protect against all scenarios. The bash manual explains that the shell won't exit if the failing command is part of a test within an if statement, inside a while/until condition, or within a && or || list, except for the final command. It also notes that using the exclamation mark (!) inverts the command's exit status. This demonstrates that strict mode alone is not sufficient and should not be considered a replacement for checking the commands that matter.

To ensure maximum safety when using AI-assisted scripting, it is recommended to begin every bash script with set -euo pipefail and to quote variables. Before running any cd, rm, or deploy commands, thoroughly read them yourself. As a precautionary measure, review the first three lines of your repository's .sh files using the command "head -3" and add strict mode to any scripts lacking it. After implementing these changes, test each script individually to guarantee they behave as intended.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in AI

Bareship: an offline-first React Native template for teams and AI agents

Bareship is a free, MIT-licensed template for bare React Native 0.87 apps. One command, npx create-bareship Acme , gives you an iOS and Android app where a like or an edit made offline is saved…

  • Bareship is a free, MIT-licensed React Native template for offline-first apps.
  • Single command creates iOS and Android apps with automatic syncing.
  • Supports AI agents with AGENTS.md, ADRs, skills, and generators.

More from Thursday 22 October →