1

I try to combine two bash files to create a dynamic configuration file. One of these files (settings.sh) contains variables and the other (config.sh) commands to execute on them.

For instance, in settings.sh I have

DATABASE='name_of_the_db'
USER='name_of_the_user'

and config.sh contains

. settings.sh
[...]
'NAME': '$DATABASE',
'USER': '$USER',
[...]

I'm struggling to create a command that would get the variable in the first file and insert it in the second, rendering a new file (settings.py) containing

'NAME': 'name_of_the_db',
'USER': 'name_of_the_user',

I tried a command like

cat < settings.py [...] EOF

as proposed here How does ` cat << EOF` work in bash? but this does obviously not perform any variable replacement.

Thank you in advance!

2
  • try to use double quotes " instead of single onnes ' in lines:[...] 'NAME': '$DATABASE', 'USER': '$USER', [...] Commented Nov 10, 2014 at 15:47
  • Thanks Roman, double quotes were missing indeed! Commented Nov 10, 2014 at 16:05

1 Answer 1

1

What I would do :

settings.sh is OK

config.sh :

. settings.sh
cat<<EOF
[...]
'NAME': "$DATABASE",
'USER': "$USER",
[...]
EOF

And to create the config.py file :

chmod +x config.sh
./config.sh > config.py

The double quotes in config.sh are mandatory, if you want the variables to be interpolated. See http://mywiki.wooledge.org/BashGuide/Practices#Quoting

Sign up to request clarification or add additional context in comments.

Comments

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.