4

I would like to import variables like username, password, host name from a config.cfg file so that I can use them inside the bash script file script.sh

user = ""
password = ""
host = ""

how can I do that, so that in the script.sh file I can use the credentials that are set in the config.cfg file to excute the following in the script:

mysqldump --user="$user" --password="$password" --host="$host" $db $tabl > "./backups/$db/$db-$tabl-$time"

2 Answers 2

6

Remove the spaces around the = signs in your config file, source it from your script, and use the so defined shell variables:

source config.cfg
echo "$user"
Sign up to request clarification or add additional context in comments.

Comments

3

You have to source it. In bash you can do that using source or ., they are equivalents. For example:

source config.cfg

or

. config.cfg

EDIT:

As correctly noted in the other answer you need to remove spaces around = for your config.cfg to work in bash.

1 Comment

just reiterating. the command need to be bash <scriptname.sh>, not sh <scriptname.sh> .. thank you for pointing this out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.