Generating a pseudorandom password or string in Linux bash

Revision history
Tags: linux bash

Define a function in e.g. ~/.bashrc

genpasswd() {
  tr -dc A-Za-z0-9 < /dev/urandom | head -c ${1:-36} | xargs
}

Where 36 is default length if no parameter is given

Usage

$ genpasswd
GVQ3ZHqrBRDzB1QwASA9uk6YsZPto2GWeRWR

$ genpasswd 7
qvPWx7N

References

If you have any comments or feedback, please send me an e-mail. (stig at stigok dotcom).

Did you find any typos, incorrect information, or have something to add? Then please propose a change to this post.

Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.