How to set environment variables in a fish sink

Can someone please tell me what is the right way to set up a bunch of environment variables in a fish shell?

In my .config / fish / config.fish file, I have a function to configure my environment variables, e.g.

function setTESTENV set -x BROKER_IP '10.14.16.216' set -x USERNAME 'foo' set -x USERPASS 'bar' end 

when I enter setTESTENV from the command line and do env on the command line, I do not see this information.

+42
shell fish
02 Sep '14 at 21:39
source share
4 answers

The variables you declare are stored in a local area inside your function.

Using:

 set -g -x 

Here is the " g " for global.

+33
Sep 02 '14 at 10:00
source share

Use Generic Variables

If the variable should be shared between all current instances of the user's fish on the current computer and saved in a restart of the shell, you must use -U or --universal :

 set -Ux FOO bar 

Using set with -g or --global does not set a constant variable between shell instances

+54
May 12, '15 at 10:18
source share

Environment Variables in Fish

I would like to add that although @JosEduSol's answer is incorrect and helps to solve the OP problem, -g sets the global scope and -x causes the specified environment variable to be exported to child processes.

The reason this fails is because @cfpete sets env vars inside the function, and the default scope will be local to this function.

+8
Feb 21 '15 at 5:24
source share

one more option is launched:

 export (cat env_file.txt |xargs -L 1) 

where env_file.txt contains strings of the format VAR=VALUE

this allows you to save variables in a format supported by other shells and tools

0
Dec 20 '17 at 9:16
source share



All Articles