What b64 does
b64 turns bytes into Base64 text and back. Give it the text as an argument, pipe data into it, or point it at a file with -i. Encoding prints the result plus one newline; decoding writes the raw bytes and nothing else, so binary files come out intact.
$ b64 "Hello, hub!"
SGVsbG8sIGh1YiE=
$ b64 -d SGVsbG8sIGh1YiE=
Hello, hub!
$ printf '\xfb\xff\xbf' | b64 --url
-_-_
$ b64 --url --no-pad "Привет"
0J_RgNC40LLQtdGC
The last two lines show the URL-safe alphabet (- and _ instead of + and /) and output without = padding, the form used in JWTs and URLs. Base64 vs Base64URL explains the difference.
Install
The install box above has a command for every package manager. In short:
| Platform | Command |
|---|---|
| macOS, Linux (Homebrew) | brew install base64hub-com/tap/b64 |
| Windows (winget) | winget install base64hub.b64 |
| Windows (Scoop) | scoop bucket add base64hub https://github.com/base64hub-com/homebrew-tap, then scoop install b64 |
| Arch Linux (AUR) | yay -S b64-bin |
| Any Unix shell | curl -fsSL https://base64hub.com/install.sh | sh |
| With Node.js or Python | npm i -g base64hub or pipx install base64hub |
| With Go | go install github.com/base64hub-com/base64hub-cli/cmd/b64@latest |
For a plain download, All downloads has .tar.gz, .zip and .deb builds for x86_64 and arm64, with a SHA256SUMS file to check them. The npm and PyPI builds install the same b64 command with the same flags.
Everyday examples
Mind the newline: echo adds one, and b64 encodes every byte it receives.
$ echo hello | b64
aGVsbG8K
$ printf 'hello' | b64
aGVsbG8=
Wrap long output, for example at 76 characters for MIME, with -w:
$ printf 'foobar' | b64 -w 4
Zm9v
YmFy
Build a data URI. The MIME type is detected from the content (PNG, JPEG, GIF, WebP, PDF, SVG, text) or set with -m:
$ printf 'Hi' | b64 --data-uri
data:text/plain;charset=utf-8;base64,SGk=
$ b64 --data-uri -i logo.png -o logo.txt
Check a string before you trust it. The verdict for invalid input goes to stderr with exit code 1. A string that starts with - needs -- in front of it so it is not read as a flag:
$ b64 --validate -- -_-_
valid (url, 3 bytes)
$ b64 --validate 'Zm9vY'
invalid: invalid length: 5 characters leave a single character in the last group
$ b64 -d 'Zm9v!'
b64: invalid character '!' at position 4
b64 vs the base64 command
Linux ships base64 from GNU coreutils, macOS ships a BSD version, and their flags differ (see Base64 in Bash for the details). b64 behaves the same on every system. The GNU column below was checked with coreutils 9.7.
| Task | GNU base64 | b64 |
|---|---|---|
| Output line length | wraps at 76 unless -w 0 | one line unless -w N |
| URL-safe alphabet | no (basenc --base64url can) | -u / --url |
Decode input with - or _ | base64: invalid input | accepted |
data: URI in or out | no | --data-uri; the prefix is stripped when decoding |
| Check a string | no | --validate with variant and byte count |
| Error details | invalid input | the bad character and its position |
Decoding in b64 is lenient on purpose: spaces, tabs and line breaks are ignored, padding is optional, and both alphabets are accepted. Anything else, such as a stray ! or a = in the middle, is an error with exit code 1. Usage errors exit with 2, so scripts can tell bad data from a bad command line.
Works offline
b64 does its work on your machine and needs no account or network connection. The Go build streams stdin in chunks, which keeps memory flat on multi-gigabyte input. For one-off strings in a browser there are the Base64 encoder and decoder on this site; the CLI docs cover every flag and exit code.
FAQ
How do I install a Base64 command line tool with Homebrew?
Run brew install base64hub-com/tap/b64. The formula lives in the base64hub-com/tap tap and installs the b64 command on macOS and Linux.
Is b64 a drop-in replacement for the base64 command?
No, the flags differ. b64 does not wrap lines unless you pass -w, decodes with -d everywhere, and adds URL-safe output, data URIs and validation. Keep using base64 in scripts that expect its exact behavior.
How do I Base64 encode a file from the command line?
b64 -i photo.jpg -o photo.b64, or b64 < photo.jpg > photo.b64. Add --data-uri to get a data:image/jpeg;base64,… string that you can paste into HTML or CSS.
Why does b64 print 'valid (either, …)'?
The string contains none of + / - _, so it is valid in both the standard and the URL-safe alphabet. standard, url and mixed appear when those characters are present.