cfpete
cfpete

Reputation: 4443

How to set environment variables in fish shell

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.

Upvotes: 159

Views: 170800

Answers (6)

Sudhakar Krishnan
Sudhakar Krishnan

Reputation: 850

I set fish environment variable from .env file in archlinux.

File: /home/sud/.config/fish/config.fish

function envsource
  echo "Setting Environment variable from::  ~/.env"
  set -f envfile "$argv"
  if not test -f "$envfile"
    echo "Unable to load $envfile"
    return 1
  end
  while read line
    if not string match -qr '^#|^$' "$line"
      set item (string split -m 1 '=' $line)
      set -gx $item[1] $item[2]
      if test -n "\$$item[2]"
        set -gx PATH $item[2] $PATH
      end
      #echo "Exported key $item[1] = $item[2]"
    end
  end < "$envfile"
end

envsource ~/.env

For reference my .env file /home/sud/.env

NODEJS=/opt/node-v22.11.0-linux-x64/bin
GO=/home/sud/go/bin
PNPM=/home/sud/.local/share/pnpm

Notes: set -gx $item[1] $item[2] Helps call >$GO will change the location to /home/sud/go/bin similar to cd /home/sud/go/bin

Upvotes: 0

Thomas Fritz
Thomas Fritz

Reputation: 340

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.

Upvotes: 1

Paolo Moretti
Paolo Moretti

Reputation: 55956

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.

Upvotes: 284

Ophir Yoktan
Ophir Yoktan

Reputation: 8449

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

Upvotes: 34

j--
j--

Reputation: 5666

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.

Upvotes: 17

JosEduSol
JosEduSol

Reputation: 5426

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

Use:

set -g -x

Here "g" is for global.

Upvotes: 103

Related Questions