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

  • http://www.shellhacks.com/en/Generating-Random-Passwords-in-the-Linux-Command-Line