Interview Prep/Shell Scripting/Troubleshooting: tools, portability, permissions and environment

On a Mac, `sed -i 's/replicas: 2/replicas: 3/' deploy.yaml` fails with `sed: 1: "deploy.yaml": extra characters at the end of d command`.

BeginnerTroubleshootingTroubleshooting: tools, portability, permissions and environment
Answer

Likely causes → BSD sed (macOS) requires a backup-suffix argument after -i. It takes your expression as the suffix and the filename as the script, so deploy.yaml is parsed as the d command followed by junk. GNU sed treats the suffix as optional and attached (-i.bak).

Diagnose → sed --version fails on macOS (BSD sed has no --version); command -v gsed shows whether GNU sed is installed.

Fix → Use a form both accept, or avoid in-place editing:

sed -i.bak 's/replicas: 2/replicas: 3/' deploy.yaml && rm -f deploy.yaml.bak   # works on GNU and BSD
sed 's/replicas: 2/replicas: 3/' deploy.yaml > deploy.yaml.tmp && mv deploy.yaml.tmp deploy.yaml

For YAML specifically, use yq -i '.spec.replicas = 3' deploy.yaml.

Prevent → Run scripts in CI on Linux and macOS if both are supported, or require GNU tools on macOS (brew install gnu-sed coreutils) and detect them at start-up.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Troubleshooting: tools, portability, permissions and environment