Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

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 have a script that dumps a MySQL database, and compresses the file. What I want to do is have another (edit) file which can change the username, password and database name. Then somehow connecting that file to this script.

In another file I want something that says for example.

username =  "username"
password = "password"
database = c2duo_db

Then in a script name mysqlbackup.sh some import the username password and database from the file.


mysqldump -u"username" -p"password" --opt c2duo_db > /home/sh/c2duo_db-`date +%d%m%Y`.sql

cd /home/sh
tar -zcvf c2duo_db-`date +%d%m%Y`.tar.gz  *.sql
share|improve this question
@Caleb : if you are using the sh shell in AIX then the source will give you the error. Please use . (dot) instead of source. – Girish Pandey Nov 18 '14 at 12:44
up vote 5 down vote accepted

You can do this easily using the source command to import the contents of another file into the current shell environment (in your case your script) and run it there.

In db.conf:


In your script:


source /path/to/db.conf
cd /home/sh
dumpfile_name=$database-$(date +%d%m%Y).sql

mysqldump -u"$username" -p"$password" --opt "$database" > $dumpfile_name
gzip $dumpfile_name


  • You should never put a space after the = assignment operator when assigning shell variables!
  • I did a couple things to cleanup your code. I didn't add the new dump to a single tar file like you were doing. I did show how you can compress an individual file. If you wanted to tar it so they were all in one archive you can do that too, but I find having individual compressed dump files quite useful.
  • I moved the cd operation to before the mysqldump command so that you wouldn't have to specify the path for the output file since that is the current directory. Just saves duplicated code.

Edit: Even with that step done, it seems like this is a half-baked solution to me. You might be interested in how you can pass values using arguments. For example you could take the hard coded path to the config file out of the script above and replace it with this:


source "$1"
cd /home/sh
[...] # rest of script as above

You could then execute it like ./mysqlbackup.sh /path/to/db.conf. You could even take this a step farther and just write it all in one script and provide all three values as arguments:


cd /home/sh
[...] # rest of script as above

...and call it like ./mysqlbackup.sh username password database.

share|improve this answer
This is exactly what I am looking for. Thanks – Shehzad009 Jul 25 '11 at 10:42

You could change the first file to:


store it as db.sed, and call it with

sed -f db.sed mysqlbackup.sh > joeSqlbackup.sh

For different users/passwords and databases you would generate different sed-scripts.

share|improve this answer

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.