Skip to content

Instantly share code, notes, and snippets.

@gwpantazes
Last active February 11, 2023 13:39
Show Gist options
  • Save gwpantazes/9c981d3ea949ccc89b34559c652e05e1 to your computer and use it in GitHub Desktop.
Save gwpantazes/9c981d3ea949ccc89b34559c652e05e1 to your computer and use it in GitHub Desktop.
Environment Variables via properties file in bash

Environment variables can be loaded from a properties file in bash

testenv.properties

foo=alpha
bar=beta

This will work because input redirection happens in the current shell.

while read line; do
    export $line
done < testenv.properties 

This will not work because piping happens in a subshell.

cat testenv.properties | while read line; do
    export $line
done

Of course, you could also make it easy for yourself by exporting the properties directly (now testenv.sh):

export foo=alpha
export bar=beta

And inject into the current shell using

source testenv.sh

Unfortunately you can't do stuff like this

export testenv.properties   # If only...
cat testenv.properties | xargs export   # Would have also been nice...
@bonthusaireddy
Copy link

You can by with the command
. file.properties
it works

@BalajiTechs
Copy link

can by with the command

Not working for me. I am running inside WSL2 (Ubuntu 20.x)

@dgilber
Copy link

dgilber commented Jul 20, 2022

If your properties file only contains properties and nothing else, this syntax works:

export $( cat testenv.properties | xargs )

@sanjul
Copy link

sanjul commented Aug 2, 2022

Figured, this works on bash:

export `cat testenv.properties`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment