Say I have a file called variables.sh
that sets two variables.
foo=bar
bar=foo
If I source
this file I can use these variables in the current shell, but if I want to use it in a second shell script I would have to export them instead, so the file would have to look like this:
export foo=bar
export bar=foo
It is possible to do some kind of source
+ export
or do I have to change variables.sh
so that there is export
before every variable is set?
Best Answer
You could source the second file, or have it source
variables.sh
. There are multiple ways to do what you wask, such as doing an eval over the result of processing the file, but the cleanest way would be to useset -a
of bash shell:Thus, you can achieve your goal with:
As noted by l0b0, you possibly want to
set +a
afterwards, so you don't continue autoexporting variables on subsequent shell code.