Skip to content

Instantly share code, notes, and snippets.

@inceabdullah
Forked from mattmc3/rand.bash
Last active August 6, 2023 07:22
Show Gist options
  • Save inceabdullah/11f90488c29d6f1b6b535a789a813b7f to your computer and use it in GitHub Desktop.
Save inceabdullah/11f90488c29d6f1b6b535a789a813b7f to your computer and use it in GitHub Desktop.
Bash - generate random base64 string
#!/bin/bash
# Generate random base64 string using openssl
# Usage: generate_base64 [length]
# length: Optional, the length of the base64 string (default is 32)
generate_base64() {
if [[ "$1" == "--help" ]]; then
echo "Usage: generate_base64 [length]"
echo " length: Optional, the length of the base64 string (default is 32)"
return
fi
local length="${1:-32}"
openssl rand -base64 "$length"
}
# Check if an argument is provided (e.g., ./generate_base64.sh 64)
if [ $# -eq 1 ]; then
generate_base64 "$1"
else
# If no argument is provided, generate the default 32-character base64 string
generate_base64
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment