linuxshellrust-cargofishbin

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


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?


Solution

  • 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: