Skip to Content
gnux

base64x

Encode or decode data in base64.

Mirrors GNU base64. Run it as base64x, or as gnux base64x.

Synopsis

base64x [OPTION]... [FILE]

GNU reference

Description

base64x encodes FILE in base64 (RFC 4648, with padding) and writes the result to standard output, or with -d decodes it. With no FILE, or when FILE is -, it reads standard input; at most one FILE is accepted. Encoded output is wrapped every 76 characters and ends with a newline; -w COLS wraps at COLS instead, and -w 0 writes one line with no newline at all. When decoding, newlines are ignored, and -i also ignores every other character outside the alphabet; any other stray character, or a truncated input, stops decoding with 'invalid input' and exit status 1, after the data decoded so far has been written.

Permissions

Needs read permission on FILE and search permission on the directories leading to it. Nothing is ever modified.

Use cases

  • Embedding binary data such as images or keys in text formats like JSON or YAML.
  • Decoding base64 found in configuration, tokens or email attachments.
  • Passing binary data through channels that only carry text.
  • Producing single-line encodings for environment variables with -w 0.
  • Inspecting encoded payloads in a pipeline.

Flags

  -d, --decode           decode data
      --help             display this help and exit
  -i, --ignore-garbage   when decoding, ignore non-alphabet characters
      --version          output version information and exit
  -w, --wrap COLS        wrap encoded lines after COLS character (default 76); use 0 to disable line wrapping

Examples

Put a small logo straight into an HTML or CSS data: URL

echox "data:image/png;base64,$(base64x -w 0 logo.png)"

Encode a short string to see what it looks like

printfx 'hello' | base64x
Output
aGVsbG8=

Decode a value you found in a config file or an API response

echox aGVsbG8= | base64x -d
Output
hello

Store a binary key in a CI secret, which only takes one line of text

base64x -w 0 deploy-key.bin > deploy-key.txt

Turn that secret back into the key file in a CI job

printfx '%s' "$DEPLOY_KEY" | base64x -d > deploy-key.bin

Wrap at 64 characters, as PEM certificates are

base64x -w 64 cert.der

Decode text pasted from an email, where line breaks and stray characters crept in

base64x -d -i pasted.txt > attachment.pdf

Send a binary file through a chat or a terminal that only carries text

base64x report.pdf > report.txt

Make sure a round trip gives back the same bytes

base64x file.bin | base64x -d > copy.bin && cksumx file.bin copy.bin

Reject a payload that is not valid base64 before using it

base64x -d payload.txt > /dev/null && echox valid

Encode in the portable gnux shell, the same on every OS

gnux shell -c 'base64x -w 0 key.bin > key.txt'

Options

Each option has a page of its own, with its usage and examples.

Exit status

StatusMeaning
0success: every operand was processed
1failure: an operand could not be processed (diagnosed on standard error), or a command-line mistake

Help in the terminal

base64x --help                # options, first examples, exit status
gnux help base64x             # this page: the full manual
gnux help base64x --OPTION    # one option in depth, with its examples

See also

On this page