bash: cannot execute binary file - what bash actually checked
You ran a file, and bash said it cannot execute it. The message sounds like a permission
problem, so most people chmod +x it and try again. That does nothing, because permission
was never the issue.
Here is what bash actually does, measured on bash 5.3.9 / Linux 7.0 (x86-64), and the four situations that produce this exact line.
The part nobody explains: bash tries twice
When you run ./file, bash asks the kernel to execute it. If the kernel refuses with
ENOEXEC, bash does not give up — it falls back to reading the file as a shell script.
That fallback is why the error is so confusing: the same ENOEXEC produces two completely
different outcomes.
| File | ./file from bash |
execve() directly |
|---|---|---|
| Plain text, mode +x | runs as a shell script (rc=0) | ENOEXEC |
| ELF for another architecture | cannot execute binary file: Exec format error (rc=126) |
ENOEXEC |
So the message means: the kernel refused, and the fallback refused too — because the content is binary, not text. Bash is telling you "this is not a script I can read either".
The three messages, and what each one means
These get quoted interchangeably. They are not the same:
| Message | Kernel error | What it means |
|---|---|---|
cannot execute binary file: Exec format error |
ENOEXEC |
Valid executable, wrong architecture (or truncated) |
cannot execute binary file (no suffix) |
ENOEXEC |
Same class; older bash, or a file that is not ELF at all |
...: No such file or directory |
ENOENT |
The file exists, but its ELF interpreter does not — dynamic binary on a system with a different libc |
...: bad interpreter: No such file or directory |
— | Shebang points at a missing program; this is a script problem |
The third row is the one that burns people: on a musl host, a glibc binary reports
no such file or directory even though ls clearly shows the file. Measured:
$ docker run --rm -v /bin/true:/x/true:ro alpine:3.20 /x/true
exec /x/true: no such file or directory
/bin/true is a glibc binary; Alpine's loader is musl. The file is there. The loader it
asks for is not.
The four real causes
1. Wrong CPU architecture (most common)
An aarch64 binary on x86-64, or the reverse. This is what you get from building on an Apple Silicon Mac and running in an amd64 container, from copying a binary between a Raspberry Pi and a laptop, or from a release page with multiple downloads.
$ file ./app
ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked
$ ./app
bash: ./app: cannot execute binary file: Exec format error
2. Wrong libc — glibc binary, musl host (or the reverse)
The architecture matches, so it is not Exec format error — instead the interpreter path
inside the binary does not exist. This produces no such file or directory, which is why
people conclude the file is missing when it is not.
3. Truncated or corrupted download
A partial download has a valid header and an invalid body. The kernel refuses with the same
ENOEXEC, and file still reports a plausible architecture:
$ file ./truncated
ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV) # header intact, body missing
$ ./truncated
bash: cannot execute binary file: Exec format error
Check the checksum before you debug anything else.
4. It is not a program at all
A data file, a .so, a zip, or a file whose name lost its extension. Running a shared
object usually gives the same message — although a few, like libc.so.6, are themselves
executable and will happily print their version instead.
Exit codes: what to test for in scripts
Measured, because CI scripts get this wrong:
| Situation | Exit code |
|---|---|
| Command not found | 127 |
| Cannot execute binary file | 126 |
| Permission denied (no +x) | 126 |
| Script fails inside | whatever it returned |
126 and 127 mean different things. 127 is "I could not find it"; 126 is "I found it and could not run it". A retry loop that treats both the same will spin forever on 126.
Diagnose in three commands
file ./app # architecture, class, static or dynamic
readelf -h ./app | grep -E 'Class|Machine|Type' # exact ELF identity
readelf -l ./app | grep interpreter # which loader it demands
uname -m # what this host actually is
If the architecture differs, you need the other build — not a chmod, not a
qemu-user hack you will regret in production, and not a copy of the loader.
If the architecture matches but the interpreter line points at a loader that is not
installed (/lib/ld-musl-x86_64.so.1 on a glibc host, or
/lib64/ld-linux-x86-64.so.2 on Alpine), you are looking at a libc mismatch: rebuild
against the target libc, or go fully static.
Fixes that match the cause
| Cause | Fix |
|---|---|
| Wrong architecture | Use the build for this CPU, or docker buildx build --platform |
| Wrong libc | Rebuild against the target libc; or link statically (-static / -static-libgcc) |
| Truncated download | Re-download; verify the published checksum |
| Not a program | Check file; a .so needs a host process, not a direct call |
| Node.js / Go toolchain | Those ecosystems ship per-arch tarballs — the package manager version is the safe one |
What does not work: chmod +x (permission was never the problem), copying a loader from
another machine (the library versions underneath will not match), and sudo (the kernel
does not care who you are).
Confirm it before you change anything
Paste the binary into ELF Inspector — it reads the header in your
browser, nothing is uploaded, and it tells you the class, machine and interpreter in one
screen. If the machine is AArch64 and your host is x86-64, you already have your answer.
If the machine matches but the interpreter does not, check which libc this host ships, and which glibc version the binary needs: the version matrix has 27 releases measured inside the official images.
Every command on this page was run on bash 5.3.9 / Linux 7.0 x86-64 and Alpine 3.20.
The patched-architecture binary was produced by changing e_machine to 0xB7 in a real
x86-64 executable, so the failure is the genuine kernel path rather than a mocked one.