Forked from earthgecko/bash.generate.random.alphanumeric.string.sh
Last active
March 22, 2018 02:34
-
-
Save nicerobot/2ec14dc35bbab5ec456c312f1bc442ad to your computer and use it in GitHub Desktop.
shell/bash generate random alphanumeric string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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