How to reference a file for variables using Bash?
The short answer
Use the source
command.
An example using source
For example:
config.sh
#!/usr/bin/env bashproduction="liveschool_joe"playschool="playschool_joe"echo $playschool
script.sh
#!/usr/bin/env bashsource config.shecho $production
Note that the output from sh ./script.sh
in this example is:
~$ sh ./script.sh playschool_joeliveschool_joe
This is because the source
command actually runs the program. Everything in config.sh
is executed.
Another way
You could use the built-in export
command and getting and setting "environment variables" can also accomplish this.
Running export
and echo $ENV
should be all you need to know about accessing variables. Accessing environment variables is done the same way as a local variable.
To set them, say:
export variable=value
at the command line. All scripts will be able to access this value.
even shorter using the dot:
#!/bin/bash. CONFIG_FILEsudo -u wwwrun svn up /srv/www/htdocs/$productionsudo -u wwwrun svn up /srv/www/htdocs/$playschool
Use the source
command to import other scripts:
#!/bin/bashsource /REFERENCE/TO/CONFIG.FILEsudo -u wwwrun svn up /srv/www/htdocs/$productionsudo -u wwwrun svn up /srv/www/htdocs/$playschool