Bash error: cannot execute binary file: Exec format error

The "cannot execute binary file: Exec format error" message appears when a script lacks a shebang, has the wrong architecture, or starts with a BOM.

Error String

bash: ./script.sh: cannot execute binary file: Exec format error

Tldr

The kernel doesn't recognize the file format. The most common causes for a shell script are a missing shebang line, a UTF-8 BOM at the start of the file, or running an architecture-specific binary on the wrong CPU.

Cause

Without a shebang, the kernel falls back to running the file as a native executable. A leading BOM (\xef\xbb\xbf) also breaks the shebang detection. For real binaries, "Exec format error" means architecture mismatch (arm64 binary on x86_64, for example).

Repro

# No shebang, no execute fallback to /bin/sh:
echo hello > run
chmod +x run
./run                          # Exec format error

Fix

cat > run <<'EOF'
#!/usr/bin/env bash
echo hello
EOF
chmod +x run
./run

# Remove a BOM if present:
sed -i '1s/^\xef\xbb\xbf//' script.sh

Explanation

Always include a shebang on the first line. Configure your editor to save shell scripts as "UTF-8 without BOM".

Faq

Q

Can I run an ARM binary on x86 Linux?

A

Only through emulation such as `qemu-user-static` with binfmt registration; otherwise get a native build.

Q

Why does bash ./script.sh work when ./script.sh does not?

A

Running it explicitly bypasses the shebang, so a missing or corrupted interpreter line stops mattering.

Deep Dive

Heading

The kernel could not recognise the format

Body

The exec failed because the file is not a binary this kernel can run: a Linux ELF on macOS, an x86-64 build on arm64, a 32-bit binary on a system without multilib, or a script whose shebang points at a missing interpreter. `file ./program` names the actual format and architecture, and `uname -m` tells you what the host expects. On macOS, `arch -x86_64` runs an Intel binary under Rosetta when it is installed.

Heading

Text files that are not really scripts

Body

The other frequent cause is a text file executed without a shebang, or with a shebang broken by Windows line endings — the interpreter path then ends in an invisible carriage return. `file` reports "with CRLF line terminators"; fix it with `dos2unix script.sh` or `sed -i 's/\r$//' script.sh`. A truncated download (an HTML error page saved as a tarball or binary) produces the same error, so verify the size and checksum before blaming the platform.

Checklist

Run `file ./program` and compare its architecture with `uname -m`.

Check for CRLF line endings in scripts.

Verify the download is complete and not an HTML error page.

Confirm the shebang interpreter exists at that exact path.