Skip to content

Reading a script before you run it

12 min

Read before you run

What to look for in a script somebody else wrote.

This is the track the rest exists for. A model's output is executed directly against your machine with your privileges, usually pasted without being read — and the thing that makes reading it possible is knowing the expansion order.

Four questions get you most of the way. Which paths does it touch? What happens if a variable it uses is empty? What happens if a filename contains a space? And does anything it runs delete, overwrite or change permissions?

Try this

The first question is the sharpest, because the shell's answer is not the obvious one. An unquoted empty variable does not become an empty argument — it disappears entirely, and the command sees one fewer argument than the author intended.

rm -rf "$DIR/" with DIR unset becomes rm -rf / in a shell without set -u, and rm -rf $DIR/* becomes rm -rf /*. Neither reports anything unusual.

Try this

set -u turns that into an error before it turns into a morning. It is one line, and it is why generated scripts should start with it.

The audit exercises give you a script an agent produced, a filesystem that looks like somebody's home directory, and one question asked before it runs: what will this do? Then you run it and watch.

$

Files

The files in the sandbox, with their modes, link counts and sizes
ModeLinksSizeName
-rw-r--r--135.bashrc
drwxr-xr-x2drafts/
-rw-r--r--168old-invoice.txt
drwxr-xr-x2inbox/
-rwxr-xr-x132build.sh
-rw-r--r--1126notes.txt
drwxr-xr-x2scratch/
-rw-r--r--177data.csv
-rw-r--r--1189report.txt
-rw-r--r--1112todo.md