Linux Command Reference ยท Archives and Compression
Linux base64 command
Reference for the Linux base64 command: encode and decode files and strings, control line wrapping, ignore garbage, make URL-safe output, and handle errors.
base64 converts binary data into plain ASCII text made from 64 characters (A-Z, a-z, 0-9, +, /) and back again. The text is about one third larger than the original, but it survives channels that only carry text: email bodies, JSON, HTTP headers, environment variables and shell scripts.
It is encoding, not encryption: anyone can decode it. Use it for transport, never for secrecy.
base64 [OPTION]... [FILE]
base64 -d [OPTION]... [FILE]
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Padding, URL-safe output and bad input behave in ways worth seeing once:
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
base64 [-d] [-i] [-w COLS] [FILE]
| Option | Description |
|---|---|
-d, --decode |
Decode instead of encode. |
-w COLS, --wrap=COLS |
Insert a newline every COLS characters when encoding. Default 76; 0 disables wrapping. |
-i, --ignore-garbage |
When decoding, skip characters outside the alphabet instead of failing. |
With no FILE, or with -, it reads standard input.
Returns: 0 on success; 1 on invalid input (it prints base64: invalid input).
Usage notes
- Use
printf, notecho, when encoding a literal:echo hello | base64encodes the trailing newline too (aGVsbG8K). - Wrapping at 76 columns suits email and PEM-style files. For single-line values (JSON, tokens,
Authorizationheaders) always use-w 0. - Padding with
=fills the output to a multiple of four characters. Some producers omit it; GNUbase64 -dstill decodes the text but exits with status 1, so add the=yourself when a script checks the status. - URL-safe Base64 replaces
+and/with-and_. GNUbase64does not have a switch for it; translate withtrbefore decoding or after encoding. - Size: every 3 input bytes become 4 output characters, so expect about 33% growth (plus newlines).
- Basic auth:
printf 'user:pass' | base64 -w 0builds the value of an HTTPAuthorization: Basicheader. It is trivially reversible, so use HTTPS. - Embedding files:
base64 -w 0 icon.pngyields a string for adata:URI; decode withbase64 -d > icon.png. - macOS and BSD use
-Dor-dand-bfor wrapping, so scripts that must run on both should test the flavor. - Alternatives:
openssl base64andxxd -p(hex) do similar jobs; hex is twice the size, Base64 one third.
Related
tr,od,xxd and hexdump,openssl,cmp- Lessons: Hex and Binary Inspection