jerrygarciuh
jerrygarciuh

Reputation: 21988

bash shell script prompting for database password

I added a shell script I use all the time in cronjobs for a new account. When I ran it (under sudo -u) I got prompted for the database password. When supplied with pass the command completed without issue.

The random password the host had assigned started with a $ and I wondered if that was issue so I modified to have pass in variable. The current script is below.

When run it still prompts for db password before successfully completing. What is wrong there?

#!/bin/sh

DBPASS="$randomlettersarehere"

/usr/bin/mysqldump --opt -h mysql.mysite.com -u myuser -p$DBPASS mydbname > "/home/mysite/backups/"`/bin/date +\%Y\%m\%d`.mysiteBackup.sql

Upvotes: 0

Views: 716

Answers (2)

jerrygarciuh
jerrygarciuh

Reputation: 21988

In case it helps someone else as derpy as I am: use single quotes if string starts with $ so bash does not interpolate sring as undefined variable

DBPASS='$randomlettersarehere'

Upvotes: 0

John Kugelman
John Kugelman

Reputation: 361585

Use single quotes to prevent $letters from being interpreted as a variable name.

DBPASS='$randomlettersarehere'

When you use double quotes the shell performs variable interpolation. With the dollar sign at the start, that means $DBPASS ends up being an empty string.

Upvotes: 2

Related Questions