Blog · How-To
How to Decode Base64 in Bash
Decode Base64 in Linux, WSL, and macOS terminals. Real commands, real output, and the two pitfalls that silently corrupt your results.
You have a Base64 string in a terminal and you need the original data back. The base64 command ships with every Unix system. No install, no dependencies, one command. But the echo newline trap and the macOS/Linux flag difference catch people every time. Here is the clean reference.
base64 -d on Linux/WSL, base64 -D on macOS. Always use echo -n when piping strings — the default echo appends a newline that gets encoded with your data and corrupts the output. For one-off decodes without a terminal, use the online Base64 decoder — paste, read, done.Decode a String from the Command Line
echo -n "SGVsbG8sIFdvcmxkIQ==" | base64 -d
# Output: Hello, World!
# macOS
echo -n "SGVsbG8sIFdvcmxkIQ==" | base64 -D
# Output: Hello, World!
The -n flag on echo is not optional here. Run echo "SGVsbG8=" | base64 -d without it and you get Hello followed by a newline character. That extra byte is invisible in most terminal output but it will break anything downstream that expects the exact original data — checksums, binary file headers, API payloads.
Decode a Base64 File
If you have a file containing a Base64 string (like encoded.b64), decoding it to a new file is cleaner than piping:
base64 -d encoded.b64 > output.png
# On macOS, same command but with -D
base64 -D encoded.b64 > output.png
The file can contain the Base64 string on a single line or spread across multiple lines — base64 -d handles both. It ignores whitespace and newlines inside the encoded data. This is useful when you are dealing with PEM certificates, which wrap Base64 at 64 characters per line. For a complete walkthrough on reconstructing files from Base64, including how to identify the file type without an extension, see our dedicated guide.
The echo Trap, Explained
echo without -n adds a trailing newline (\n, 0x0A) to whatever you pipe. That newline gets Base64-decoded along with your string. If you are decoding text, the output looks fine except for an extra blank line. If you are decoding binary data — an image header, a cryptographic key, a PDF — that extra byte corrupts the file in ways that range from "won't open" to "appears to work but is silently wrong."
Safer alternative to echo: use printf instead. It does not append a newline by default:
# Output: Hello (no trailing newline, guaranteed)
Linux vs macOS: Why the Flag Difference Exists
Both systems use the same base64 utility, but macOS inherited the BSD version, where -d means something else entirely. The BSD flag for decode is uppercase -D. Linux (GNU coreutils) uses lowercase -d. If you get a "base64: illegal option" error on Mac, you used the wrong case.
The portable fix: skip base64 entirely and use openssl base64 -d, which accepts the lowercase flag on both platforms. Or use Python's python3 -c "import base64, sys; print(base64.b64decode(sys.argv[1]).decode())" — same on every OS.
Frequently Asked Questions
Why does my decoded output have an extra blank line?
The echo command appended a newline before piping. Use echo -n to suppress it, or switch to printf which does not add a newline by default.
I get "base64: illegal option -- d" on macOS. What gives?
macOS uses the BSD version of base64. The decode flag is uppercase -D, not lowercase -d. Or use openssl base64 -d, which accepts the lowercase flag on both Linux and Mac.
Is it safe to pipe untrusted Base64 strings into the terminal?
Decoding a Base64 string itself is safe — it produces bytes, not executable commands. But if you then redirect those bytes into a file and execute that file, you are running whatever was encoded. Same caution as opening any file from an untrusted source. Also: piping arbitrary binary output directly to your terminal can corrupt the display. Redirect to a file with > output.bin and inspect the file, rather than printing raw bytes to stdout.
Skip the terminal. Decode Base64 instantly online.
Paste, read, done. No flags to remember, no newline traps.
Open the Decoder