export
is the answer.
Interactive exercices
As shell is interactive, you could try inline!
$ mkdir conf && printf 'MINIENTREGA_%s="%s"\n' FECHALIMITE 2011-03-31 FICHEROS \
"informe.txt programa.c" DESTINO ./destino/entrega-prac1 >conf/prac1
$ set -- prac1
$ while read -r line; do export $line; done <"conf/$1"
bash: export: `programa.c"': not a valid identifier
$ while read -r line; do LANG=C export "$line"; done <"conf/$1"
$ echo "$MINIENTREGA_FICHEROS"
"informe.txt programa.c"
Note the double quotes!
source
alias .
$ set -- prac1
$ . "conf/$1"
$ echo "$MINIENTREGA_FICHEROS"
informe.txt programa.c
Ok, then now what's about export
export
command tell shell to export shell variables to environment... So you have to export script variables before use them is any subprocess (like ruby
, python
, perl
or even another shell
script.
Cleaning previous operations for further demos
$ declare +x MINIENTREGA_FECHALIMITE MINIENTREGA_FICHEROS MINIENTREGA_DESTINO
$ unset MINIENTREGA_FECHALIMITE MINIENTREGA_FICHEROS MINIENTREGA_DESTINO
So from an interactive shell, simpliest way to try this is to run another shell:
$ set -- prac1
$ . "conf/$1"
$ bash -c 'declare -p MINIENTREGA_FICHEROS'
bash: line 1: declare: MINIENTREGA_FICHEROS: not found
$ export MINIENTREGA_FECHALIMITE MINIENTREGA_FICHEROS MINIENTREGA_DESTINO
$ bash -c 'declare -p MINIENTREGA_FICHEROS'
declare -x MINIENTREGA_FICHEROS="informe.txt programa.c"
Sample shell wrapper for exporting variables
Minimal wrapper, without security concern (care when sourcing script editable by other users!!).
#!/bin/sh
while IFS== read -r varname _;do
case $varname in
*[!A-Za-z0-9_]* | '' ) ;;
* ) export $varname ;;
esac
done <conf/$1
. conf/$1
busybox sh -c 'set | grep MINIENTREGA'
Run with prac1
as argument, should produce:
MINIENTREGA_DESTINO='./destino/entrega-prac1'
MINIENTREGA_FECHALIMITE='2011-03-31'
MINIENTREGA_FICHEROS='informe.txt programa.c'
In fine
This two operation can be done in any order indifferently. The only requirement is that both operations are done before you try to run any subprocess.
You could even do both operation together, by exporting in your config file, for sample:
export MINIENTREGA_FECHALIMITE="2011-03-31"
export MINIENTREGA_FICHEROS="informe.txt programa.c"
export MINIENTREGA_DESTINO="./destino/entrega-prac1"