Skip to content

Instantly share code, notes, and snippets.

@ericwastaken
Last active April 16, 2022 06:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericwastaken/1f0ede8a6e7b8fc3ff57528ff46b9850 to your computer and use it in GitHub Desktop.
Save ericwastaken/1f0ede8a6e7b8fc3ff57528ff46b9850 to your computer and use it in GitHub Desktop.
An example Bash script that reads variables from an environment file

An example Bash script that reads variables from an environment file

This will load variables into a BASH SCRIPT from an environment file.

Create an environment file with the following structure. Call it "secrets.env" but it can be called anything!

MY_SECRET="Some Secret"

Then, in your script, include the following:

#!/bin/bash

# Load env file
set -o allexport
[[ -f secrets.env ]] && source secrets.env
set +o allexport

# Now all the variables inside secrets.env are local to the script
echo "Your secret is ${MY_SECRET}"

License

This content is shared under a Creative Commons Attribution-ShareAlike 4.0 International.

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