How to load a .env file's environment variables into both Bash and Fish shells on startup?

133 Views Asked by At

I want to have a .env file with a bunch of name-value pairs, like:

VAR1=xxx
VAR2=yyy

# sample C include dir use case
INCLUDE_DIR=/etc/include/xxx

I want these variables to be available in both bash and fish shells, such that I can access them in either shell at any time like echo $VAR1.

1

There are 1 best solutions below

1
HumpbackWhale194 On
  1. Create a file in ~/.env (for example) with the variables in the format above.
  2. Add the following to the bottom of ~/.bash_rc:
source ~/.env
  1. Add the following to the bottom of ~/.config/fish/config/fish:
export (envsubst < ~/.env | sed '/^\s*#/d; /^$/d')

This command reads the .env file, removes blank lines and comments, and then sets them as environment variables.