Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set environment variables from file of key/value pairs

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.

like image 220
hugo19941994 Avatar asked Oct 12 '13 07:10

hugo19941994


People also ask

Are environment variables key value pairs?

The form of an environment variable is a key/value pair. The key() method takes a value as its parameter. It then gets the name of the first environment variable with such a value.

Which file required to set the environment variable is?

On each node, in the installation owner user profile file, set the environment variables ORACLE_BASE and ORACLE_HOME , and ORACLE_SID ; also add ORACLE_HOME/bin to the path environment variable.


2 Answers

-o allexport enables all following variable definitions to be exported. +o allexport disables this feature.

set -o allexport source conf-file set +o allexport 
like image 30
user4040650 Avatar answered Sep 21 '22 03:09

user4040650


This might be helpful:

export $(cat .env | xargs) && rails c 

Reason why I use this is if I want to test .env stuff in my rails console.

gabrielf came up with a good way to keep the variables local. This solves the potential problem when going from project to project.

env $(cat .env | xargs) rails 

I've tested this with bash 3.2.51(1)-release


Update:

To ignore lines that start with #, use this (thanks to Pete's comment):

export $(grep -v '^#' .env | xargs) 

And if you want to unset all of the variables defined in the file, use this:

unset $(grep -v '^#' .env | sed -E 's/(.*)=.*/\1/' | xargs) 

Update:

To also handle values with spaces, use:

export $(grep -v '^#' .env | xargs -d '\n') 

on GNU systems -- or:

export $(grep -v '^#' .env | xargs -0) 

on BSD systems.


From this answer you can auto-detect the OS with this:

export-env.sh

#!/bin/sh  ## Usage: ##   . ./export-env.sh ; $COMMAND ##   . ./export-env.sh ; echo ${MINIENTREGA_FECHALIMITE}  unamestr=$(uname) if [ "$unamestr" = 'Linux' ]; then    export $(grep -v '^#' .env | xargs -d '\n')  elif [ "$unamestr" = 'FreeBSD' ]; then    export $(grep -v '^#' .env | xargs -0)  fi  
like image 77
Silas Paul Avatar answered Sep 19 '22 03:09

Silas Paul