156

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 this information.

0

5 Answers 5

275

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 can set them using -U or --universal. For example:

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.

Universal variables will be stored in the file ~/.config/fish/fish_variables as of Fish 3.0. In prior releases, it was ~/.config/fish/fishd.MACHINE_ID, where MACHINE_ID was typically the MAC address.

6
  • How can I export a new path to $PATH in bash? Are there any advantages of setting variables in fish in this way?
    – GoingMyWay
    Commented Apr 24, 2022 at 12:46
  • 6
    This is a horrible answer. Universal variables should never be used for env vars. Universal variables also shouldn't be used for values that can change each time you start a fish shell or that might be different for concurrently running shells. I can't believe this answer got so many upvotes. Commented Aug 8, 2022 at 0:24
  • Have to agree with @KurtisRader. Right tools for the right job, which universal variables are not here.
    – CJK
    Commented Aug 8, 2022 at 5:22
  • I think it's just because many people are landing on this answer when googling how to set shell variables. The main paragraph makes pretty clear that you only want to use universal variables when they need to be shared between multiple shells and preserved across restarts of the shell. Commented Aug 8, 2022 at 13:51
  • @PaoloMoretti: I understand the point you're making. However, in my four decades of using UNIX I've never seen a situation where I want a change to an env var in one shell instance to be automatically reflected in all other instances. At least not where that behavior is more likely to cause problems than benefit. Note that I used, and contributed changes to, the Fish shell for several years. And every single time someone asked about doing set -Ux they didn't understand the implications and the -U option was always the wrong thing. Commented Aug 9, 2022 at 0:56
103

The variables you are declaring are keep in a local scope inside your function.

Use:

set -g -x

Here "g" is for global.

0
31

another option is to run:

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

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

this has the benefit of keeping the variables in a format supported by other shells and tools

3
  • 7
    In case you want to avoid comments you can: export (grep "^[^#]" .env |xargs -L 1)
    – Guillermo
    Commented Sep 3, 2020 at 22:03
  • it's temporary (for session only), isn't it?
    – Danil
    Commented Jul 10, 2023 at 10:04
  • @Danil right, if you want to make it persistent you need to add it to the startup script Commented Jul 16, 2023 at 19:04
17

Environment Variables in Fish

I would like to add that, while @JosEduSol's answer is not incorrect and does help solve the OP problem, -g is only setting the scope to be global, while -x is causing the specified environment variable to be exported to child processes.

The reason the above fails, is because @cfpete is setting the env vars inside a function and the default scope will be local to that function.

0

The syntax you are using for setting environment variables is correct, but the fact that you are setting them as part of a function and later calling that function by hand is what prevents your environment variables from being exposed to other programs such as env because as @j-- mentioned the local scope of your environment variables is then limited to that function.

If you instead define the environment variables directly in ~/.config/fish/config.fish without using a function, they get applied to every fish terminal session you open and to all programs you run from them:

# ~/.config/fish/config.fish
set -x BROKER_IP '10.14.16.216'
set -x USERNAME 'foo'
set -x USERPASS 'bar'

And it similarly works if you define them in a function and call that function in ~/.config/fish/config.fish:

# ~/.config/fish/config.fish
function setTESTENV
      set -x BROKER_IP '10.14.16.216'
      set -x USERNAME 'foo'
      set -x USERPASS 'bar'

setTESTENV

That function can also be in a separate fish script in ~/.config/fish/functions, which can help you keep your config.fish file organized if you need to set a lot of different environment variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.