bashpasswords

Creating a password in Bash


I currently have a password that is being generated, that is a hash of the date. I'd like to increase this generated password's security, by adding in some uppercase, lower case, numeric, and special characters, all to a minimum length of 10 characters.

What I currently have is below. As you can see, it assigns the output to my PASSWORD variable.

PASSWORD=$(date +%s | sha256sum | base64 | head -c 15)

I'm unsure if I can do this inline, or, if I need to create a function within my bash script to suit? Is that even possible?

Many thanks in advance.


Solution

  • With tr and head:

    password=$(tr -dc 'A-Za-z0-9!?%=' < /dev/urandom | head -c 10)
    
    echo "$password"
    

    Output (example):

    k?lmyif6aE
    

    tr reads bytes via stdin from Linux‘ special random device /dev/urandom and removes all bytes/characters but A to Z, a to z, 0 to 9 and !?%=. tr sends its output via stdout to head‘s stdin. head cuts output after 10 bytes.