Sometimes you need to create a password or other random string of letters and numbers in a script or on the command line. I normally use the excellent tool pwgen, but sometimes that is not available on the system you are working on.
When pwgen isn’t available, you can sanitize the output of /dev/urandom so that you only get text and numbers. The following command will output a 10-character string:
cat /dev/urandom | tr -d -c [:alnum:] | head -c 10
cat /dev/urandomThis outputs /dev/urandom to stdout; you can’t use this as is contains lots of data that isn’t a letter or number.trThe translate command-dDeletes the characters that match the filter.-cReverses the deletion, instead printing the characters that it would have deleted and deleting the others.[:alnum:]Letters and numbers.
head -c 10Prints the first 10 bytes.
Change the head -c 10 to make the output as long as you need it to be.