Base64 Encoding on the Command Line — Linux, macOS, and Windows
Encode and decode Base64 on the command line using base64, openssl, and PowerShell. Includes piping files, multiline input, URL-safe Base64, and common use cases like encoding...
The base64 command is available on Linux and macOS. Windows uses PowerShell’s [Convert]::ToBase64String. Here are the essential commands for encoding, decoding, and working with files.
Use the base64 decoder and encoder to encode and decode in the browser.
Linux / macOS: base64 command
# Encode a string:
echo -n "Hello World" | base64
# SGVsbG8gV29ybGQ=
# The -n flag prevents echo from adding a trailing newline
# Without -n: echo "Hello" | base64 gives a different result
# Decode:
echo "SGVsbG8gV29ybGQ=" | base64 --decode
# Hello World
# macOS uses -D instead of --decode:
echo "SGVsbG8gV29ybGQ=" | base64 -D
# Encode a file:
base64 image.png > image.b64
# Decode back to file:
base64 --decode image.b64 > output.png
# Encode and decode in one pipeline:
cat image.png | base64 | base64 --decode > output.png
Encode API credentials
# HTTP Basic Auth header = Base64(username:password):
echo -n "alice:p@ssw0rd!" | base64
# YWxpY2U6cEBzc3cwcmQh
# Use in curl:
curl -H "Authorization: Basic YWxpY2U6cEBzc3cwcmQh" https://api.example.com/
# Or let curl handle it:
curl -u alice:p@ssw0rd! https://api.example.com/
Using openssl for Base64
# Encode:
echo -n "Hello World" | openssl base64
# SGVsbG8gV29ybGQ=
# Decode:
echo "SGVsbG8gV29ybGQ=" | openssl base64 -d
# Encode file (openssl wraps at 64 chars by default):
openssl base64 -in image.png -out image.b64
# Decode file:
openssl base64 -d -in image.b64 -out output.png
# No line wrapping:
openssl base64 -A -in image.png # -A = no newlines
URL-safe Base64
Standard Base64 uses + and / which need escaping in URLs. URL-safe Base64 uses - and _ instead:
# Encode URL-safe:
echo -n "Hello World" | base64 | tr '+/' '-_' | tr -d '='
# SGVsbG8gV29ybGQ (no trailing =)
# Decode URL-safe:
echo "SGVsbG8gV29ybGQ" | tr '-_' '+/' | awk '{
n=length($0) % 4
if (n==2) $0=$0"=="
else if (n==3) $0=$0"="
print
}' | base64 --decode
Windows PowerShell
# Encode string to Base64:
$text = "Hello World"
$bytes = [System.Text.Encoding]::UTF8.GetBytes($text)
[Convert]::ToBase64String($bytes)
# SGVsbG8gV29ybGQ=
# Decode Base64 to string:
$encoded = "SGVsbG8gV29ybGQ="
$decoded = [Convert]::FromBase64String($encoded)
[System.Text.Encoding]::UTF8.GetString($decoded)
# Hello World
# Encode a file:
$bytes = [System.IO.File]::ReadAllBytes("C:\image.png")
[Convert]::ToBase64String($bytes) | Out-File -Encoding ascii image.b64
# Decode file:
$encoded = Get-Content image.b64 -Raw
$bytes = [Convert]::FromBase64String($encoded)
[System.IO.File]::WriteAllBytes("C:\output.png", $bytes)
Multiline encoded output
By default, base64 on Linux wraps output at 76 characters. Remove wrapping with -w 0:
# Without -w 0: output has newlines every 76 chars
echo -n "Long text..." | base64
# No line breaks (needed for JSON, headers, etc.):
echo -n "Long text..." | base64 -w 0
Common use cases
# 1. Embed small image in HTML (data URI):
echo "data:image/png;base64,$(base64 -w 0 icon.png)"
# 2. Encode secret for environment variable:
echo -n "my-api-key:my-api-secret" | base64 -w 0
# Store as: API_CREDENTIALS=bXktYXBpLWtleT...
# 3. Encode binary file for email (MIME):
uuencode -m image.png image.png
# 4. Check if a string is valid Base64:
echo "SGVsbG8=" | base64 --decode > /dev/null 2>&1 && echo "valid" || echo "invalid"
Related tools
- Base64 Encoder/Decoder — encode and decode Base64 online
- How Base64 Works — Base64 algorithm explained
- Base64 Image Encoding — embed images in HTML/CSS
Related posts
- Base64: How It Actually Works Under the Hood — Base64 is everywhere — in JWTs, data URLs, email attachments. This is the byte-l…
- When You Should NOT Use Base64 Encoding — Base64 is the duct tape of the web — and like real duct tape, it's used in place…
- Base64 Data URLs — Embed Images, Fonts, and Files in HTML and CSS — Base64 data URLs encode binary files as text strings for embedding directly in H…
- Base64 Encode Online — Encode Text and Files to Base64 Instantly — Base64 encoding converts binary data into printable ASCII text. It's used in ema…
- Base64 Image Encoding — Embed Images in HTML, CSS, and JSON — Base64 image encoding converts image files to text strings for embedding in HTML…
Related tool
Encode and decode Base64 strings and files. Client-side, safe for sensitive data.
Written by Mian Ali Khalid. Part of the Dev Productivity pillar.