Interview Prep/Shell Scripting/Troubleshooting: strict mode, exit codes, pipelines and traps

A script dies immediately with exit code 1 and no output. The first command is `read -r VERSION < VERSION`. The file contains `1.4.2`.

IntermediateTroubleshootingTroubleshooting: strict mode, exit codes, pipelines and traps
Answer

Likely causes → The VERSION file has no trailing newline (common when written with printf or by an editor/CI tool). read still fills the variable but returns 1 because it hit end-of-file before a newline, and set -e exits.

Diagnose →

tail -c 4 VERSION | od -c              # no \n at the end
bash -c 'set -e; read -r v &lt; VERSION; echo "got $v"'; echo "rc=$?"

Fix → Accept "partial last line" as success, or avoid read for single values:

read -r VERSION &lt; VERSION || [[ -n $VERSION ]]
VERSION=$(&lt;VERSION)                       # strips trailing newlines, no read status issue

Prevent → Write files with a trailing newline (printf '%s\n'), and use $(&lt;file) for one-value files.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Troubleshooting: strict mode, exit codes, pipelines and traps