SpaceAce's dotenv for fish shell
A rough equivalent of sourcing .env files in bash
Load shell variables from .env files. As a bonus, unset and cleanup those variables when you're done, so they don't clutter up your shell.
It is common in Linux and Linux-like shells to store key=value pairs in files which can be easily loaded into the shell's environment. Personally, I deal with dozens of projects on a daily basis which require login credentials, configuration variables, and other information to exist in the shell environment*. If they do not, the software will not work. When developing those applications, I need an easy way to load the required variables into the shell.
In bash, this is solved by source .env. Fish, however, does not use the "key=value" format that bash uses for setting environment variable. If you attempt to source a standard .env file in fish, you will generate errors. The fish function in this repository solves that problem.
* This is for development purposes, and all sensitive information is fake.
source dotenv.fish
dotenv
I mean, yeah. That's pretty much it. If you know what .env files are, you're ready to go.
- Clone this repository, or copy/download the file dotenv.fish to your machine.
- Insert the function into your shell. Any of the following work.
- Temporary solutions; these will only exist for the current shell session, and will disappear when you exit the shell or reboot.
source dotenv.fish
- Or
- Paste the contents of dotenv.fish directly into your shell.
- Permanent solutions
- Manual
- If it does not already exist, create the directory
[your home folder]/.config/fish/functions - Copy
dotenv.fishto the above location
- If it does not already exist, create the directory
- Automatic, sort of
- If you have already followed one of the steps under "temporary solutions," you can use
funcsave dotenvfrom the command-line to permanently store the function. Fish will put it in the same place as you did in the "manual" steps.
- If you have already followed one of the steps under "temporary solutions," you can use
- Manual
- Temporary solutions; these will only exist for the current shell session, and will disappear when you exit the shell or reboot.
If using the manual solution, dotenv will not be available until you start a new shell. Assuming a standard installation, you can do so by typing fish in your existing shell. Alternatively, you can use one of the temporary solutions to load dotenv into your current environment. This is only necessary for your current session; subsequent sessions will already have dotenv available.
Dotenv accepts a small number of command-line options to modify its behavior.
-
-h
- Displays a brief overview of its usage
-
-q
- Suppresses error message generated when dotenv cannot locate the environment file(s).
-
-u
- Unsets the variables found in the environment file. This is very useful for cleanup when you no longer want or need the variables hanging around in your shell.
-
-x
- Force export of the variables and values found in the environment file. This makes them available to sub-processes of the shell. This is probably what you want if you're using the .env file to configure a process you're going to run from the shell.
- Note: If "export" is included in the .env file, such as
export variable=value, that variable will be exported regardless of whether or not-xis used. The-xflag is used to force the export of all variables in a given .env file.
-
.env, .env.dev, etc
- Anything other than the options above is treated as an environment file. By default, dotenv looks for a file called
.envin the current directory. However, you can specify any other name(s) you like.
- Anything other than the options above is treated as an environment file. By default, dotenv looks for a file called
dotenv
Reads the file .env in the current directory, and creates shell variables based on what it finds there.
dotenv -u
Reads the file .env in the current directory, and , rather than setting the variables it finds there, it unsets them, removing them from the current environment.
dotenv -x
Does the same as dotenv, but exports the variables so they are available to sub-processes launched from the current shell.
dotenv .env.dev .env.local
Does the same as dotenv, but reads from the files .env.dev and .env.local rather than from .env, and creates all the variables it finds in both files. If the variables share names, the values in the second file will overwrite the values in the first file.
You may specify as many files as you like, subject to the limits of your computer, and reality as we know it.
dotenv -x env.local
Sets and exports variable found in .env.local
dotenv -u env.local
Does the same as dotenv -u, except it reads the variable names from .env.local. rather than from .env.
dotenv .file_that_does_not_exist
Will complain about the missing file.
dotenv -q .file_that_does_not_exist
Will not complain about the missing file.
grep -v '^\s*#' dotenv.fish