Interview Prep/Shell Scripting/Troubleshooting: syntax, parsing and interpreter errors

A Docker container exits immediately with `exec /entrypoint.sh: exec format error`. The script runs fine with `sh /entrypoint.sh`.

IntermediateTroubleshootingTroubleshooting: syntax, parsing and interpreter errors
Answer

Likely causes → The kernel cannot execute the file directly: it has no shebang line (or the shebang is not on the very first line, e.g. after a blank line or a UTF-8 BOM), or it has CRLF endings. Docker's exec-form ENTRYPOINT calls execve(), which needs a valid #! line.

Diagnose →

head -c 20 entrypoint.sh | od -c     # must start with "#  !  /"; a BOM shows as 357 273 277
file entrypoint.sh

Fix → Make #!/bin/sh (or #!/usr/bin/env bash) the first bytes of the file, strip any BOM and CRLF, and chmod +x.

Prevent → .gitattributes for LF, ShellCheck in CI (it flags a missing shebang with SC2148), and a container smoke test in the pipeline.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Troubleshooting: syntax, parsing and interpreter errors