{"slug": "the-ai-written-shell-script-that-ran-from-the-wrong-folder-and-exited-0-set-euo", "title": "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", "summary": "A developer demonstrated that a four-line bash script with an unset BUILD_DIR variable continued past a failed cd, ran its deploy line, and exited 0 until strict mode was added. Prepending set -euo pipefail made the script halt on the first error with exit code 1 and an unbound variable message. The writeup also notes that set -e does not trigger on failures inside if conditions, while/until tests, non-final commands in && or || lists, or negated commands, and recommends agents generate scripts with strict mode and quoted variables while humans review cd, rm and deploy lines.", "body_md": "**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`.\n\n``` bash\n#!/usr/bin/env bash\nset -euo pipefail\ncd \"$BUILD_DIR/out\"\n```\n\n**What each flag does (bash's own `help set`):**\n\n`-e`: \"Exit immediately if a command exits with a non-zero status.\"`-u`: \"Treat unset variables as an error when substituting.\"`-o pipefail`: \"the return value of a pipeline is the status of the last command to exit with a non-zero status, or zero if no command exited with a non-zero status.\" Without it, `false | true` counts as success.\n**Where `-e` still won't save you (GNU Bash manual):** the shell does not exit when the failing command is part of the test in an `if`, inside a `while`/` until` condition, any command in a `&&` or `||` list except the final one, or a command inverted with `!`. The demo shows `if false; then …; fi` carrying on under `set -e`. Don't treat strict mode as a substitute for checking the commands that matter.\n\n**For AI-assisted scripting:** ask your agent to start every bash script with `set -euo pipefail`, and to quote variables. Then read any `cd`, `rm` or deploy line yourself before running it.\n\n**Do this now:** run `head -3` on your repo's `.sh` files and add strict mode to any that lack it. Then test each one, because scripts that relied on ignored errors will now stop.\n\n*From Penloom Studio's free AI lessons: [penloomstudio.com](https://penloomstudio.com/landing-cogloom-motion.html)*", "url": "https://wpnews.pro/news/the-ai-written-shell-script-that-ran-from-the-wrong-folder-and-exited-0-set-euo", "canonical_source": "https://dev.to/penloom_studio_829b7817d3/the-ai-written-shell-script-that-ran-from-the-wrong-folder-and-exited-0-set-euo-pipefail-and-349", "published_at": "2026-10-22 15:38:24+00:00", "updated_at": "2026-10-22 15:44:23.385297+00:00", "lang": "en", "topics": ["ai-tools", "developer-tools"], "entities": ["Penloom Studio", "GNU Bash"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/the-ai-written-shell-script-that-ran-from-the-wrong-folder-and-exited-0-set-euo", "markdown": "https://wpnews.pro/news/the-ai-written-shell-script-that-ran-from-the-wrong-folder-and-exited-0-set-euo.md", "text": "https://wpnews.pro/news/the-ai-written-shell-script-that-ran-from-the-wrong-folder-and-exited-0-set-euo.txt", "jsonld": "https://wpnews.pro/news/the-ai-written-shell-script-that-ran-from-the-wrong-folder-and-exited-0-set-euo.jsonld"}}