Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import variables into bash script from a config file

Tags:

bash

shell

mysql

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"
like image 995
iTikoz Avatar asked Sep 20 '25 18:09

iTikoz


2 Answers

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"
like image 121
Renaud Pacalet Avatar answered Sep 22 '25 10:09

Renaud Pacalet


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.

like image 24
Arkadiusz Drabczyk Avatar answered Sep 22 '25 08:09

Arkadiusz Drabczyk