How Do You Encode Base64 on Linux?
The base64 command is part of GNU coreutils and is available on all Linux distributions. To encode a string, pipe it to base64. Use echo -n to avoid including the trailing newline in the encoded output:
echo -n 'Hello World' | base64
# Output: SGVsbG8gV29ybGQ=
# Wrong — includes the newline character in encoding
echo 'Hello World' | base64
# Output: SGVsbG8gV29ybGQK (different result — K at end encodes the newline)
How Do You Decode Base64 on Linux?
Use the -d flag to decode. On Linux (GNU base64), the flag is lowercase -d. On macOS (BSD base64), the flag is uppercase -D:
echo 'SGVsbG8gV29ybGQ=' | base64 -d
# Output: Hello World
# Decode to stdout (no trailing newline added)
echo 'SGVsbG8gV29ybGQ=' | base64 --decode
How Do You Encode and Decode Files in Bash?
Use file redirection to encode or decode whole files without piping:
# Encode a binary file to Base64
base64 image.png > image.b64
# Encode and suppress line wrapping (single line)
base64 -w 0 image.png > image.b64
# Decode Base64 file back to binary
base64 -d image.b64 > decoded-image.png
# One-liner: encode file and save inline
base64 -w 0 certificate.pem > certificate.b64
How Do You Disable Line Wrapping in bash?
By default, GNU base64 wraps output at 76 characters per line (MIME format). Use -w 0 to produce a single continuous line — required when embedding Base64 in JSON, HTML, or environment variables:
# Single-line output (required for JSON/HTML embedding)
echo -n 'Hello World' | base64 -w 0
# Output: SGVsbG8gV29ybGQ= (no line breaks)
# Kubernetes secret value (must be single line)
kubectl create secret generic my-secret \\
--from-literal=password=$(echo -n 'mypassword' | base64 -w 0)
How Do You Use Base64 in bash Scripts?
Base64 encoding is commonly used in bash scripts for API authentication, Kubernetes secrets, and embedding binary assets:
#!/bin/bash
# Basic Auth header for API calls
USERNAME="admin"
PASSWORD="secret"
CREDENTIALS=$(echo -n "$USERNAME:$PASSWORD" | base64 -w 0)
curl -H "Authorization: Basic $CREDENTIALS" https://api.example.com/data
# Encode a file as a variable
IMAGE_B64=$(base64 -w 0 logo.png)
echo '{"image": "'"'"'$IMAGE_B64'"'"'"}' | curl -X POST -d @- https://api.example.com/upload
# Decode a secret from an environment variable
echo "$BASE64_SECRET" | base64 -d > /tmp/secret.key
Linux vs macOS Base64 Commands — Key Differences
| Operation | Linux (GNU) | macOS (BSD) |
|---|---|---|
| Encode string | echo -n 'str' | base64 | echo -n 'str' | base64 |
| Decode string | echo 'b64' | base64 -d | echo 'b64' | base64 -D |
| No line wrap | base64 -w 0 | base64 -b 0 |
| Encode file | base64 file.bin | base64 -i file.bin |
| Decode file | base64 -d file.b64 | base64 -D -i file.b64 |
For macOS-specific usage see the Base64 on Mac guide. For browser-based encoding without a terminal, use the Text Encoder or File Encoder.
Frequently Asked Questions
How do I encode Base64 on Linux?
Run echo -n 'text' | base64. For files: base64 file.bin > file.b64. Use -w 0 to disable line wrapping.
How do I decode Base64 on Linux?
Run echo 'BASE64' | base64 -d. For files: base64 -d input.b64 > output.bin.
Why does base64 -d not work on Mac?
macOS uses -D (uppercase). Linux uses -d (lowercase). See the macOS Base64 guide for Mac-specific commands.
How do I Base64 encode without newlines in bash?
Use base64 -w 0: echo -n 'text' | base64 -w 0. This produces a single continuous string, required for JSON embedding and Kubernetes secrets.