Generate random password with bash

24年 10月 15日 Tuesday
92 words
1 minutes

The following script will generate 16 character length random string:

bash
#!/usr/bin/env bash

password=""
while [ ${#password} -lt 16 ]; do
	generated_str="$(dd if=/dev/random bs=1 count=32 2>/dev/null | tr -dc 'A-Za-z0-9!@#$%^&*()_+')"
	password="$password$generated_str"
done;

echo -n "${password:0:16}"

It will concatenate and fill the string password with pre-defined characters until the length is sufficient enough to be a standard password length, 16.

The concatenation comes into play to speed up the process, so it will be much faster than the other way around.

example usage (assuming the script was saved as a file with name genpwd):

text
genpwd | xclip -sel clip

Title:Generate random password with bash

Author:ReYuki

Link:https://www.reyuki.site/posts/generate-password-with-bash [copy]

Last updated:


This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. You are free to share and adapt it, as long as you give appropriate credit, don’t use it for commercial purposes, and distribute your contributions under the same license. Provided under license CC BY-NC-SA 4.0