How to permanently set env across every session in fish shell?
Asked Answered
M

1

6

I install the cargo building environment. I use cargo install exa to install exa (an alternatives of command ls). Executable file is put in $HOME/.cargo/bin. I use fish shell set -gx PATH $PATH $HOME/.cargo/bin, and then I can use command exa on current session.

But when I open a new session, I cannot execute exa unless set -gx ... again.

I look in the options gx:

--global  -g     (Make variable scope global)
--export  -x  (Export variable to subprocess)

Does not global mean every session? Could someone help me pls?

Metalliferous answered 28/3, 2021 at 14:35 Comment(1)
Just a friendly, neighborhood reminder that questions on Stack Overflow should be regarding "specific coding, algorithm, or language problems." Your question would likely be better suited for Unix & Linux Stack, but please familiarize yourself with that site's rules for posting as well. I'd recommend deleting it here and moving it over before the mods close it. It won't count "against" your rep if you do it yourself before downvotes.Verruca
I
16

Doesnot global means every session?

It doesn't. It's "global" as opposed to "local". From the fish documentation:

Global variables are specific to the current fish session, and will never be erased unless explicitly requested by using set -e.

In general, what you want is to just put the set -gx into ~/.config/fish/config.fish. That's fish's configuration file.

Fish also has "universal" variables, which are stored persistently, but they interact awkwardly with exporting so I wouldn't recommend it.

For $PATH specifically, fish offers the fish_user_paths variable that it adds automatically, so you can run

set -U fish_user_paths $fish_user_paths $HOME/.cargo/bin

once, interactively, and fish will take care of it. This is a universal variable, but fish takes care to add it to $PATH when necessary (for each component it checks if it's already there and such). Do not put this into config.fish, or it will add one $HOME/.cargo/bin every time you start a fish, and so it would balloon the variable.

To recap:

  • For global variables, put the set statement into config.fish
  • For universal variables, execute it manually
  • For $PATH, use $fish_user_paths for your customizations
Immortal answered 28/3, 2021 at 16:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.