Base64 decode on Linux and macOS.
Every Linux box has a Base64 decoder, and so does every Mac. They take slightly different options, and both are fussy in ways that bite in scripts. Here are the commands, the differences and the fixes.
Updated 9 November 2026
Run echo 'SGVsbG8=' | base64 -d, or base64 -d input.b64 > output.bin for a file. --decode works on both Linux (GNU coreutils) and macOS, so use it in scripts that run on both. For URL-safe input use basenc --base64url -d on Linux, or convert first with tr '_-' '/+'.
Decode a string
echo 'SGVsbG8sIHdvcmxkIQ==' | base64 -d
# Hello, world!
base64 -d <<< 'SGVsbG8sIHdvcmxkIQ==' # same thing, bash/zsh here-string
On the decode side, a trailing newline from echo is fine; base64 ignores line breaks. (When encoding, use echo -n or printf, or the newline gets encoded too.)
The decoded output has no trailing newline of its own, so your prompt may appear on the same line. That's normal.
Decode a file
base64 -d input.b64 > output.pdf
file output.pdf # check what you got: "PDF document, version 1.7"
file reads the magic bytes and tells you the real type, whatever the extension says. For a quick look at the first bytes, use head -c 16 output.pdf | od -An -tx1 (xxd and hexdump -C work too where installed).
You can also tell the type from the Base64 itself before decoding: head -c 12 input.b64. JVBERi0 means PDF, iVBORw0KGgo PNG, H4sI gzip. See the file-signature table.
Linux vs macOS
Linux distributions ship GNU coreutils base64. macOS ships its own version with different flags. The overlap is smaller than it looks:
| Task | Linux (GNU) | macOS |
|---|---|---|
| Decode | -d, --decode | -D, --decode; recent versions also accept -d, but on older ones -d meant debug |
| Input file | positional: base64 -d file | -i file or positional |
| Output file | redirect with > | -o file or redirect |
| Ignore invalid characters | -i, --ignore-garbage | not available |
| No line wrapping (encode) | -w 0 | default (use -b N to wrap) |
On Linux -i means ignore garbage. On macOS it means input file. A script that runs base64 -d -i file means different things on the two systems. Use --decode and a positional file name, and it works on both.
"base64: invalid input"
GNU base64 stops at the first problem and exits with status 1. Note that it still prints whatever it decoded before the error, so check the exit code in scripts rather than whether there was output:
echo 'SGVs!bG8=' | base64 -d
# Helbase64: invalid input
The usual causes and fixes:
- Stray characters such as quotes, a
data:prefix or HTML around the string. Strip them, or on Linux add--ignore-garbage(-di) to skip anything outside the alphabet. - Missing padding.
echo 'SGVsbG8' | base64 -dprintsHelloand then the error. Add the=back (below). - URL-safe characters
-and_. Translate them first, or usebasenc.
A small function that handles all three for tokens:
b64d() {
local s
s=$(printf '%s' "$1" | tr -d ' \n\r' | tr '_-' '/+')
while [ $(( ${#s} % 4 )) -ne 0 ]; do s="$s="; done
printf '%s' "$s" | base64 --decode
}
b64d 'eyJhbGciOiJIUzI1NiJ9' # {"alg":"HS256"}
URL-safe Base64 with basenc
GNU coreutils 8.31 and later include basenc, which knows the URL-safe alphabet:
echo '_-7d' | basenc --base64url -d | od -An -tx1
# ff ee dd
basenc still wants correct padding, so unpadded JWT segments need the padding loop above. It isn't available on macOS by default; use the tr version there.
openssl base64
OpenSSL is installed almost everywhere and has its own decoder. It has one trap: by default it expects line breaks every 64 or 76 characters, and with a long single-line string it can stop early without any error. In a test with OpenSSL 3.0, a 4,000-character line decoded to 696 bytes instead of 3,000. Add -A for single-line input:
openssl base64 -d -A -in input.b64 -out output.bin
When the input is wrapped (PEM blocks, MIME parts), leave -A off.
Other one-liners
python3 -m base64 -d input.b64 > output.bin # anywhere Python 3 is installed
perl -MMIME::Base64 -ne 'print decode_base64($_)' < input.b64 > output.bin
To decode the payload of a JWT on the command line, combine cut with the function above: b64d "$(echo "$TOKEN" | cut -d. -f2)".
Not sure the decode worked? Paste the original string into the decoder. It shows the text, the hex bytes and the detected file type side by side, so you can compare it with what your code produced. Nothing is uploaded; it runs in your browser.
Published 9 November 2026. Spot an error? Email contactus@base64decode.tools.
Common questions.
Pipe the string into base64 -d, for example echo 'SGVsbG8=' | base64 -d. For a file, run base64 -d input.b64 > output.bin.
GNU coreutils on Linux uses -d. Older macOS versions only decode with -D (there, -d was a debug flag), while recent versions accept both. --decode works on Linux and macOS, so it is the safest choice for scripts.
The string contains characters outside the Base64 alphabet, is missing its = padding, or uses the URL-safe characters - and _. Remove stray characters (or use --ignore-garbage on Linux), add the padding back, and translate - and _ to + and / with tr.
Without -A, openssl expects the input to be wrapped in lines of up to 76 characters. A long single line can be cut short silently. Use openssl base64 -d -A for single-line input.
On Linux with coreutils 8.31 or later, use basenc --base64url -d. Otherwise translate the characters first with tr '_-' '/+' and add any missing = padding.