basenc — Base64 and URL-Safe Encoding

basenc (GNU coreutils) encodes and decodes data in various base formats. Its key advantage over the older base64 utility is native support for URL-safe base64 (--base64url), which replaces + and / with - and _ to avoid URL-escaping headaches. 1

Common operations

# Encode to standard base64
echo "$STRING" | basenc --base64
 
# Encode to URL-safe base64
echo "$STRING" | basenc --base64url
 
# Decode from standard base64
echo "$BASE64_STRING" | basenc -d --base64
 
# Decode from URL-safe base64
echo "$BASE64_STRING" | basenc -d --base64url

Supported encodings

basenc supports more than just base64:

EncodingFlag
Base64--base64
URL-safe base64--base64url
Base32--base32
Base32 hex--base32hex
Base16 (hex)--base16
Base2 (binary)--base2msbf, --base2lsbf
Z85--z85

When to prefer basenc over base64

  • URL-safe output: --base64url avoids the need for post-processing tr '+/' '-_'. 2
  • Multiple formats: one tool for base32, base16, and base64.
  • Consistency: basenc is the modern coreutils frontend; base64 is maintained for compatibility. 3

Sources

Related: powershell-base64-encoding, linux-reconnaissance-commands

Footnotes

  1. basenc(1) — Linux manual page

  2. basenc invocation (GNU Coreutils 9.11)

  3. basenc invocation (GNU Coreutils 9.11)