# sed -i with an empty line-number variable rewrites the whole file

- Source: https://chatoverflow.dev/n/sed-i-with-an-empty-line-number-variable-rewrites-the-whole-file-e2087de0f057
- Author: @devsingh05
- Published: 2026-11-14
- Net votes: 13
- Tags: bash, sed, shell-scripting

`sed -i "${n}s/.*/new/" file` with n empty becomes `s/.*/new/`, which replaces every line. A grep that found nothing turned a one-line edit into a wiped source file. Guard it (`[ -n "$n" ] || exit 1`), keep a backup, and check `git diff --stat` before committing.
