Reference

Base64 Decode & Encode on Mac

How to decode and encode Base64 strings and files on macOS using the built-in base64 terminal command.

How Do You Decode Base64 on Mac?

macOS includes a built-in base64 command in Terminal. To decode a Base64 string, pipe it to base64 -D (capital D — this is the macOS-specific flag):

echo 'SGVsbG8gV29ybGQ=' | base64 -D
# Output: Hello World

Note: macOS uses -D (uppercase) for decode. Linux uses -d (lowercase). This is the most common source of errors when copying commands between platforms. For a browser-based alternative with no terminal required, use the Base64 Text Decoder.

How Do You Encode a String to Base64 on Mac?

Pipe the string to base64 without any flags. Use echo -n to avoid encoding the trailing newline character, which would corrupt the output:

echo -n 'Hello World' | base64
# Output: SGVsbG8gV29ybGQ=

Without -n, the newline at the end of echo output is included in the encoding, producing a different result. Always use echo -n when encoding strings.

How Do You Encode and Decode Files on Mac?

Use the -i (input) and -o (output) flags to work with files instead of pipes:

# Encode a file to Base64 and save to output.txt
base64 -i image.png -o image.b64

# Decode a Base64 file back to binary
base64 -D -i image.b64 -o decoded-image.png

# Encode and print to stdout
base64 /path/to/file.pdf

# Decode from file and print to stdout
base64 -D /path/to/encoded.txt

The file-based approach is better for large files than piping, as it avoids loading everything into memory. For a no-terminal alternative, use the Base64 File Encoder or File Decoder.

What Is the Difference Between base64 -d and base64 -D on Mac?

This is the most common Base64 confusion on macOS:

FlagPlatformAction
base64 -DmacOS (BSD)Decode Base64
base64 -dLinux (GNU)Decode Base64
base64 (no flag)BothEncode to Base64
base64 -w 0Linux onlyEncode without line wrapping
base64 -b 0macOS onlyEncode without line wrapping

If you see an error like base64: invalid option -- d on macOS, switch to -D.

How Do You Disable Line Wrapping on Mac?

By default, macOS base64 wraps output at 76 characters per line (MIME format). To get a single-line Base64 string with no line breaks, use -b 0:

# Single-line output (no newlines in Base64)
echo -n 'Hello World' | base64 -b 0
# Output: SGVsbG8gV29ybGQ=

# With line wrapping (default, every 76 chars)
base64 -i large-file.bin -o large-file.b64

Use -b 0 when you need the Base64 string for embedding in JSON, HTML, or CSS. Use the default (with wrapping) for MIME email attachments.

How Do You Use Base64 with Clipboard on Mac?

Combine base64 with pbcopy and pbpaste to work with the clipboard:

# Encode clipboard contents to Base64 and copy back
pbpaste | base64 -b 0 | pbcopy

# Decode Base64 from clipboard and copy decoded text back
pbpaste | base64 -D | pbcopy

# Encode a file to Base64 and copy to clipboard
base64 -i image.png -b 0 | pbcopy

Frequently Asked Questions

How do I decode Base64 on Mac?

Run echo 'YOUR_BASE64' | base64 -D in Terminal. Use capital D — macOS uses -D for decode, not -d like Linux.

Why does base64 -d fail on Mac?

macOS uses BSD base64 which requires -D (uppercase) for decode. Linux uses GNU base64 which uses -d (lowercase). Switch to -D to fix the error.

How do I encode a file to Base64 on Mac?

Run base64 -i /path/to/file.png -o output.txt to write to a file, or base64 /path/to/file.png to print to stdout.

Can I use Base64 on Mac without Terminal?

Yes — use the browser-based tools: Image Encoder, Text Encoder, or File Encoder. No installation required.