Why a script breaks when edited mid-run
When Bash runs a script, it keeps the file open and reads it from disk in chunks — one command or block at a time — tracking the current byte offset. If the file is overwritten in place and lines above change length, the next read starts in the middle of a command:
before edit: byte 120 ──▶ docker compose up -d
after shift: byte 120 ──▶ cker compose up -d ← Bash reads from mid-line
Best case, the script fails with command not found or a syntax error. Worst case, it runs a truncated command (for
example, with a cut-off path).
During git merge or git checkout, git often unlinks the old file and writes a new one (a new inode), leaving
Bash’s open file descriptor on the old copy. You can’t rely on that: overwriting with cp, editing in place, or
syncing onto the same inode shifts the bytes right under the running shell.
How to protect it: wrap everything in main() and exit
Wrap all working code in a main() function (Bash functions) and invoke it on the last line together with exit.
Before — commands at the top level of the file:
#!/bin/bash
set -u
cd /opt
git fetch -q origin main || exit 0
git merge -q --ff-only origin/main || exit 1
docker compose -f memos/docker-compose.yaml up -d --wait
After — body inside main(), as in /opt/scripts/deploy.sh from Deploy without SSH:
#!/bin/bash
set -u
cd /opt
main() {
git fetch -q origin main || exit 0
git merge -q --ff-only origin/main || exit 1
docker compose -f memos/docker-compose.yaml up -d --wait
}
main "$@"; exit $?
Once Bash sees main() {, it has to read and parse the entire function body up to the closing } into memory before
git merge ever runs. main "$@"; exit $? exits right after the function returns: even if the file on disk grew
longer, Bash never reads past that line.
The updated script pulled in by git merge runs on the next invocation (for example, on the next deploy.timer tick
in systemd timers instead of cron). If the script sources external files after git merge, those are read from disk in their new
state — source them before updating the repo as well.
If it doesn’t work
The script fails with a strange error like cker: command not found or unexpected EOF right after the file updates
- Cause
- Commands sat at the top level outside any function, and the file was overwritten in place: Bash’s read offset landed in the middle of a line.
- Fix
- Move all code into
main() { … }and end the file with a single line:main "$@"; exit $?.
Updated deploy.sh in git, the commit was pulled onto the server, yet that run executed the old version
- Cause
- That’s expected: Bash loaded the previous
main()into memory beforegit mergeso it wouldn’t read the changed file mid-run. - Fix
- Nothing to fix — the new
deploy.shtakes effect on the next timer run or after a manualsudo systemctl start deploy.service.
After main "$@" without exit at the bottom, a fragment of a new line ran or a syntax error occurred
- Cause
- The overwritten file grew longer than before: after returning from
main, Bash kept reading from the old offset and hit the appended tail. - Fix
- Write
main "$@"; exit $?on one line so the process exits before reading any further.
How to check
Reproduce overwriting a running script with a temporary file in /tmp:
cat <<'EOF' > /tmp/self-edit.sh
#!/bin/bash
main() {
echo "before"
printf '# extra comment line that shifts bytes\n%.0s' {1..5} > /tmp/self-edit.sh
echo "after"
}
main "$@"; exit $?
EOF
bash /tmp/self-edit.sh
# before
# after
rm -f /tmp/self-edit.sh
Both lines (before and after) print without error even though the script overwrote its own file mid-execution.
Without the main() wrapper, the same overwrite aborts execution or throws a syntax error on the shifted lines.