Bash error: "cannot execute binary file: Exec format error"
"Exec format error" means the binary or script cannot be run on this CPU architecture or is missing a valid shebang. Diagnose with file and fix it.
Error String
bash: ./program: cannot execute binary file: Exec format error
Tldr
This means the kernel could not load the file as an executable — either it is a compiled binary built for a different CPU architecture (e.g. an ARM binary on x86_64), or a script with no valid shebang line, or a shell script mistakenly marked and executed as a binary. Check the file type with `file` and re-run through the correct interpreter or architecture.
Cause
Every executable file starts with a format the kernel recognizes: ELF headers for native binaries, or "#!" for interpreted scripts. Exec format error happens when neither matches — you copied an ARM/aarch64 binary onto an x86_64 machine (common with Docker images built on Apple Silicon), you tried to execute a script that lost its shebang line, or the shebang points somewhere invalid so the kernel falls back to interpreting garbage as a binary.
Repro
$ file ./app
app: ELF 64-bit LSB executable, ARM aarch64, ...
$ ./app
bash: ./app: cannot execute binary file: Exec format errorFix
#!/usr/bin/env bash
set -euo pipefail
# Confirm the mismatch
file ./app
uname -m
# Option 1: run under emulation (Docker buildx / qemu)
docker run --platform linux/amd64 myimage ./app
# Option 2: rebuild for the target architecture
GOARCH=amd64 GOOS=linux go build -o app
# Option 3: for scripts, ensure a valid shebang exists
head -c2 script.sh # should print "#!"
sed -i '1i #!/usr/bin/env bash' script.sh # if missingExplanation
Use `file <path>` first — it tells you immediately whether you have a script, an ELF binary, and for which architecture. On Apple Silicon Macs, cross-building Docker images without --platform is the most common cause; use `docker buildx build --platform linux/amd64,linux/arm64`.
Faq
Q
Why does this happen after a Docker build on an M1/M2 Mac?
A
Docker Desktop on Apple Silicon defaults to building arm64 images. If that image is later run on an x86_64 host (many CI runners and cloud VMs), the binaries inside cannot execute, producing Exec format error. Build with --platform linux/amd64 or use buildx for multi-arch images.
Q
Can this happen with plain shell scripts?
A
Yes, if the file has no shebang or the shebang got corrupted (e.g. truncated by a bad copy). The kernel then tries to interpret the first bytes as an ELF header and fails immediately.
Q
How do I check what architecture a binary needs?
A
Run `file <path>` — it prints the architecture (e.g. x86-64, ARM aarch64) directly in its output, which you compare against `uname -m` on the target machine.