Skip to content

Instantly share code, notes, and snippets.

@Sebi2020
Created April 9, 2020 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sebi2020/f271a2846844bedc158181df936314e7 to your computer and use it in GitHub Desktop.
Save Sebi2020/f271a2846844bedc158181df936314e7 to your computer and use it in GitHub Desktop.
Allows you to run programs with an environment specified by `.env` files
#!/bin/bash
if [ $# -lt 1 ]; then
echo "Missing parameter!"
exit 1
fi
env $(cat .env | xargs) $1
@Sebi2020
Copy link
Author

Sebi2020 commented Apr 9, 2020

Save the file in your home directory. For example as ~/.runwithenv.sh and make it executable (chmod u+x ~/.runwithenv.sh). Finally add the following alias at the end of your ~/.bashrc:

alias wenv=~/.runwithenv.sh

The script expects the .env file to be in the same path as your executable.

The .env file should have the following format:

ENV_VARIABLE_NAME_A=ENV_VALUE_A
ENV_VARIABLE_NAME_B=ENV_VALUE_B

and so on. Now you're able to execute scripts with the environment specified by invoking any executable like this:

wenv ./myprogram

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