Issue
TL;DR: How do I export a set of key/value pairs from a text file into the shell environment?
For the record, below is the original version of the question, with examples.
I'm writing a script in bash which parses files with 3 variables in a certain folder, this is one of them:
MINIENTREGA_FECHALIMITE="2011-03-31"
MINIENTREGA_FICHEROS="informe.txt programa.c"
MINIENTREGA_DESTINO="./destino/entrega-prac1"
This file is stored in ./conf/prac1
My script minientrega.sh
then parses the file using this code:
cat ./conf/$1 | while read line; do
export $line
done
But when I execute minientrega.sh prac1
in the command line it doesn't set the environment variables
I also tried using source ./conf/$1
but the same problem still applies
Maybe there is some other way to do this, I just need to use the environment variables of the file I pass as the argument of my script.
Solution
Problem with your approach is the export
in the while
loop is happening in a sub shell, and those variable will not be available in current shell (parent shell of while loop).
Add export
command in the file itself:
export MINIENTREGA_FECHALIMITE="2011-03-31"
export MINIENTREGA_FICHEROS="informe.txt programa.c"
export MINIENTREGA_DESTINO="./destino/entrega-prac1"
Then you need to source in the file in current shell using:
. ./conf/prac1
OR
source ./conf/prac1
Answered By - anubhava Answer Checked By - Robin (WPSolving Admin)