RSSAmplifier

Bash Prompt · Jan 15, 2026

How to Create a Random Password on the Command Line

0
Sign in to vote or save

Elliot Cooper · Bash Prompt

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/urandom This outputs /dev/urandom to stdout; you can’t use this as is contains lots of data that isn’t a letter or number.
  • tr The translate command
    • -d Deletes the characters that match the filter.
    • -c Reverses the deletion, instead printing the characters that it would have deleted and deleting the others.
    • [:alnum:] Letters and numbers.
  • head -c 10 Prints the first 10 bytes.

Change the head -c 10 to make the output as long as you need it to be.

Read the original on bash-prompt.net

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.