Philip Hutchins

Head in the cloud...

Create a Random Alpha Numeric String in Bash

Many times in a bash script there is the need to generate a random string of characters. For instance, I have a docker container that outputs bogus logs for testing and I wanted to ensure that each log line was uniquie.

You can generate a random alpha numeric string in upper/lower case or just lowercase with the following one liners.

32 Character Upper & Lower

1
RAND_STRING=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)

32 Character Lower Only

1
RAND_STRING=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)