X Xerobit

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...

Mian Ali Khalid · · 4 min read
Use the tool
Base64 Encoder / Decoder
Encode and decode Base64 strings and files. Client-side, safe for sensitive data.
Open Base64 Encoder / Decoder →

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 posts

Related tool

Base64 Encoder / Decoder

Encode and decode Base64 strings and files. Client-side, safe for sensitive data.

Written by Mian Ali Khalid. Part of the Dev Productivity pillar.