views:

43

answers:

1

I'd like to include a config file in my bash script with 2 conditions: 1) the config file name is constructed on-the-fly and stored in variable, and 2) in case if config file doesn't exist, the script should fail:

config.cfg:

 CONFIGURED=yes

test.sh:

#!/bin/sh
$CFG=config.cfg

echo Source command doesn't work here:
[ -f $CFG ] && ( source $CFG ) || (echo $CFG doesnt exist; exit 127)
echo $CONFIGURED

echo ... but works here:
source $CFG
echo $CONFIGURED

What's wrong in [...] statement?

+7  A: 

( ... ) runs the commands in a separate subshell. If you want to run the commands in the same shell if possible then use { ... ; } instead.

Ignacio Vazquez-Abrams
Thanks Ignacio! that's just what I need. [+]
Igor
...though in this specific case there's no need for either `( ... )` or `{ ... ; }`
Charles Duffy