Skip to content
Tutorials Center
My learning

Topics

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.

Terminal
base64 [OPTION]... [FILE]
base64 -d [OPTION]... [FILE]

Try it

Bash Encode, decode and round-trip

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:

Bash Padding, URL-safe variant and errors

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

Terminal
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, not echo, when encoding a literal: echo hello | base64 encodes the trailing newline too (aGVsbG8K).
  • Wrapping at 76 columns suits email and PEM-style files. For single-line values (JSON, tokens, Authorization headers) always use -w 0.
  • Padding with = fills the output to a multiple of four characters. Some producers omit it; GNU base64 -d still 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 _. GNU base64 does not have a switch for it; translate with tr before 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 0 builds the value of an HTTP Authorization: Basic header. It is trivially reversible, so use HTTPS.
  • Embedding files: base64 -w 0 icon.png yields a string for a data: URI; decode with base64 -d > icon.png.
  • macOS and BSD use -D or -d and -b for wrapping, so scripts that must run on both should test the flavor.
  • Alternatives: openssl base64 and xxd -p (hex) do similar jobs; hex is twice the size, Base64 one third.