how to set environment variables in fish shell

cfpete picture cfpete · Sep 2, 2014 · Viewed 55.6k times · Source

Can someone please tell me what's the correct way to set a bunch of environment variables in the fish shell?

In my .config/fish/config.fish file, I have a function to setup my environment variables like so

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

when I type from the command prompt setTESTENV and do a env in the command line, I don't see these information.

Answer

Paolo Moretti picture Paolo Moretti · May 12, 2015

Use Universal Variables

If the variable has to be shared between all the current user fish instances on the current computer and preserved across restarts of the shell you have to use -U or --universal:

set -Ux FOO bar

Using set with -g or --global doesn't set the variable persistently between shell instances


Note:

Do not append to universal variables in config.fish file, because these variables will then get longer with each new shell instance. Instead, simply run set -Ux once at the command line. And it will be stored in the file .config/fish/fishd.MACHINE_ID, where MACHINE_ID is typically your MAC address.