Generate random string

Most of the recommendations online are about the combination of the tr command with /dev/urandom or openssl

Openssl

openssl rand -base64 12

openssl rand -hex 12

get only digits and letters: tr -dc A-Za-z0-9 </dev/urandom | head -c 13 ; echo ''

special characters tr -dc ‘A-Za-z0-9!"#$%&’'’()*+,-./:;<=>?@[]^_`{|}~’ </dev/urandom | head -c 13 ; echo

if issues with tr imput

LC_ALL=C tr -dc ‘A-Za-z0-9!"#$%&’'’()*+,-./:;<=>?@[]^_`{|}~’ </dev/urandom | head -c 13 ; echo

Here is how, I do it. It generates 10 characters random string. You can optimize it by replacing the “fold”, with other string processing tools. cat /dev/urandom | tr -dc ‘a-zA-Z0-9’ | fold -w 10 | head -n 1

This outputs all of the ASCII printable characters - from 32 (space) to 126 (tilde, ~). The password length can be controlled with the head’s -c flag. There are also other possible character sets in tr (to not include the space, just characters 33-126, use [:graph:]).

</dev/urandom tr -cd “[:print:]” | head -c 32; echo

base64 < /dev/urandom | tr -d ‘O0Il1+/’ | head -c 44; printf ‘\n’

https://unix.stackexchange.com/questions/230673/how-to-generate-a-random-string/230676#230674