4

I'm using the following line to create a randomly generated password in a bash script, but it doesn't look like the string is actually being set to the variable.

#Generate random password
PASSWORD=date+%s|sha256sum|base64|head -c 32;
echo $PASSWORD

When I reference $PASSWORD later in this script, drush complains that I didn't supply a password. To test, I've tried a few "echoes" to confirm my suspicion. Does it look there's something off in my assignment statements?

2
  • Use syntax VAR=$(command).
    – enedil
    Jul 18, 2014 at 14:58
  • A tip on 'random' things that might be used as a password, check man mktemp and note the --dry-run option, it is stated as "unsafe" with regard to creating temporary files... but may suit your needs quite well.
    – Hannu
    Jul 18, 2014 at 15:09

4 Answers 4

6

You have to use a command substitution:

#Generate random password
PASSWORD=$(date +%s|sha256sum|base64|head -c 32)
echo $PASSWORD

then it should work.

Prefer the "$" sign, read more here: Using backticks or dollar in shell scripts

2
  • Darn, wish I could mark two answers!
    – sean
    Jul 18, 2014 at 14:59
  • You can upvote ;-)
    – Hannu
    Jul 18, 2014 at 15:10
5

You need to use a command substitution quotation, like

PASSWORD=`date +%s|sha256sum|base64|head -c 32`
echo $PASSWORD

or you can do it as,

PASSWORD=$(date +%s|sha256sum|base64|head -c 32)
echo $PASSWORD
1
  • 3
    The second type is preferred, as it is more visual/visible and also allows nesting to be done easily.
    – Hannu
    Jul 18, 2014 at 15:00
5

Generating a password just based on the current time is quite bad.

As you want a 32-character password, I would do something like this:

PASSWORD=$(head -c 16  /dev/random | md5sum | cut -f 1 -d\ )
1
  • or just use pwgen
    – tomodachi
    Jul 28, 2014 at 13:54
1

as not having the reputation to comment I'm forced to add a non answer on this tread

Like @Ángel I was skeptical about generating a password based on time so I did some test

with the command proposed by @Ángel : the password have an average entropy of : 125bits

with the command based on time as proposed by @souravc : the average password entropy is : 175bits

so it's not that bad to generate a password based on time after all ;)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .