Fish shell - Load environment variables from file

In many places, you can find environment files, with the following structure:

1
2
FOO1=BAR1
FOO2=BAR2

When you try to evaluate, this file using source command, you get an error with the fish shell.

1
2
$ source web.env                                                   
Unsupported use of '='. In fish, please use 'set FOO1 BAR1'.

This is very annoying, so I’ve decided to write a function that can read this file and load those variables. Here is how you can use it:

1
2
3
$ posix-source web.env
$ echo $FOO1
BAR1

The source code of this function. Enjoy:

1
2
3
4
5
6
7
$ cat .config/fish/functions/posix-source.fish                      
function posix-source
for i in (cat $argv)
set arr (echo $i |tr = \n)
set -gx $arr[1] $arr[2]
end
end

Photo credits: Banner, Thumbnail

Comments