Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save nicerobot/2ec14dc35bbab5ec456c312f1bc442ad to your computer and use it in GitHub Desktop.

Select an option

Save nicerobot/2ec14dc35bbab5ec456c312f1bc442ad to your computer and use it in GitHub Desktop.
shell/bash generate random alphanumeric string
#!/bin/bash
# bash generate random alphanumeric string
randomized() { cat /dev/urandom | tr -dc ${2:-"A-HJ-NP-Za-km-z2-9%^&*()_+?><~;"} | fold -w ${1:-32} | head -n 1; }
# LC_CTYPE=C supports macos. It is done in a subshell so as not to pollute the environment.
random_string() { echo $(export LC_CTYPE=C; randomized ${@}); }
random_hex() { random_string ${1:-4} 'a-f0-9'; }
random_hexu() { random_string ${1:-4} 'A-F0-9'; }
# bash Version 4 UUID
UUID4() { local uuid=($(random_hexu 8) $(random_hexu 4) $(random_hexu 4) $(random_hexu 4) $(random_hexu 12)); echo $(IFS='-'; echo "${uuid[*]}"); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment