Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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;

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 '14 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 '14 at 15:09
up vote 5 down vote accepted

You need to use a command substitution quotation, like

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

or you can do it as,

PASSWORD=$(date +%s|sha256sum|base64|head -c 32)
share|improve this answer
The second type is preferred, as it is more visual/visible and also allows nesting to be done easily. – Hannu Jul 18 '14 at 15:00

You have to use a command substitution:

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

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 '14 at 14:59
You can upvote ;-) – Hannu Jul 18 '14 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 '14 at 13:54

Your Answer


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.