Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
    
Use syntax VAR=$(command). –  enedil Jul 18 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 at 15:09

3 Answers 3

up vote 5 down vote accepted

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
share|improve this answer
3  
The second type is preferred, as it is more visual/visible and also allows nesting to be done easily. –  Hannu Jul 18 at 15:00

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

share|improve this answer
    
Darn, wish I could mark two answers! –  sean Jul 18 at 14:59
    
You can upvote ;-) –  Hannu Jul 18 at 15:10

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\ )
share|improve this answer
    
or just use pwgen –  tomodachi Jul 28 at 13:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.