Encode

str_url_encode

Percent-encode a string for use in a URL (RFC 3986). Unreserved characters (A-Z a-z 0-9 - _ . ~) are passed through unchanged.

Example

str_url_encode "hello world"   # => hello%20world
str_url_encode "foo=bar&baz"   # => foo%3Dbar%26baz

Arguments

  • ... (string): The string to encode

Exit codes

  • 0: Always

Output on stdout

  • URL-encoded string

str_url_decode

Decode a percent-encoded URL string. Plus signs are decoded as spaces (application/x-www-form-urlencoded convention).

Example

str_url_decode "hello%20world"   # => hello world
str_url_decode "foo%3Dbar"       # => foo=bar

Arguments

  • ... (string): The URL-encoded string to decode

Exit codes

  • 0: Always

Output on stdout

  • Decoded string

str_from_base64

Decode a base64-encoded string. Tries base64, openssl, uudecode in order.

Example

str_from_base64 "aGVsbG8gd29ybGQ="   # => hello world

Arguments

  • ... (string): The base64-encoded string to decode

Exit codes

  • 0: Success
  • 1: No suitable tool found

Output on stdout

  • Decoded string

str_escape

Escape a string for safe use as a shell argument. Uses bash's printf %q format, which produces output suitable for reuse as input to the shell.

Example

str_escape "hello world"     # => hello\ world
str_escape "it's a test"     # => it\'s\ a\ test

Arguments

  • ... (string): The string to escape

Exit codes

  • 0: Always

Output on stdout

  • Shell-escaped string

str_to_hex

Convert a string to its hexadecimal representation. Requires xxd.

Arguments

  • $1 (string): The string to convert

Exit codes

  • 0: Always

Output on stdout

  • Hex-encoded string (no spaces, lowercase)