Skip to content

Instantly share code, notes, and snippets.

@hebrides
Created February 5, 2022 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hebrides/3fadc588f7ec08a4cbf794449b927165 to your computer and use it in GitHub Desktop.
Save hebrides/3fadc588f7ec08a4cbf794449b927165 to your computer and use it in GitHub Desktop.
Generate random alphanumeric text DIGITS long.
#!/bin/bash
# This is a simplified version of this script: https://gist.github.com/earthgecko/3089509
# "Random numbers in a range, more randomly distributed than $RANDOM
# which is not very random in terms of distribution of numbers."
# You may want to add a reference to your .bashrc file, e.g.:
# alias rnd="path/to/script/rnd.sh"
# macOS fix, ref: https://github.com/projectzeroindia/CVE-2019-19781/issues/3
export LC_CTYPE=C
if (( $# != 1 )); then
echo "Script syntax: rnd <number of digits>, e.g. rnd 256";
echo "Defaulting to 32 digits";
DIGITS=32
else
DIGITS=$1
fi
# generate random alphanumeric text DIGITS long
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w $DIGITS | head -n 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment