Yeda AI Tips · #089

Español

Run Shell Scripts Through ShellCheck First

Your AI assistant writes bash with the same quoting bugs you do. It learned shell from the same scripts that taught humans to write rm -rf $DIR/ — and just like a human, it will hand you code that works on the happy path and detonates on a filename with a space. The fix isn't better prompting. It's a pipeline: lint, then format, then test — in that order.

Why lint before test

Most shell bugs aren't logic bugs. They're static defects: an unquoted variable expansion, a [ ] comparison that breaks on empty strings, a bashism inside a #!/bin/sh script. A behavior test can pass 100 times while an unquoted variable waits for the first path with a space in it.

That's the trap with AI-generated scripts specifically: the model produces something plausible, you run it once, it works, you ship it. A linter reads the script the way the shell will parse it, not the way it happened to run — and it catches the whole defect class at once instead of one crash at a time.

ShellCheck is the standard tool here — a static analysis tool for sh/bash scripts, recommended by the Google Shell Style Guide "for all scripts, large or small." Every finding has a wiki page with the exact bad pattern and the fix, which also makes it perfect paste-back material for your AI assistant.

The three-stage pipeline

StageToolCommandCatches
1. LintShellCheckshellcheck script.shQuoting, portability, dead code, misspelled vars
2. Formatshfmtshfmt -l -w script.shInconsistent indentation and layout
3. BehaviorBatsbats test/What the script actually does

Two ShellCheck findings pay the whole bill by themselves:

Then shfmt normalizes formatting (-l lists files that would change, -w rewrites in place), and Bats — a TAP-compliant testing framework for Bash 3.2+ — covers behavior with .bats files, a run helper for invoking commands, and setup/teardown hooks.

Test the paths the AI never imagines

Power-user moves

Resources

Building an AI feature? Yeda AI designs, audits, and ships production LLM systems.

Talk to us · Read the blog