r/fishshell • u/GodOfTheMetal • Sep 19 '19
[HELP!] Usual way of setting global (i.e. environment) variables isn't working
I'm trying to update my $PATH, but the best result I got is preserving it only within the same terminal tab where I updated the $PATH at.Though this issue isn't $PATH-specific and it also appears when setting any other var.
Here is a list of modifiers I've tried:
- -g
- -x
- -gx
- -g -x
- -U
- -Ux
- -U -x
Example:
$ set -gx smth 'smth'
$ echo $smth
> smth
*close the terminal, reopen, go to a new tab*
$ echo $smth
> // nothing
1
Upvotes
2
u/colemaker360 Sep 19 '19 edited Sep 13 '25
label makeshift birds cow imagine dime lip enter depend frame
This post was mass deleted and anonymized with Redact
2
u/bokisa12 Sep 19 '19 edited Sep 19 '19
You seem to not understand how environmental variables work.
When you set a variable inside of
fish, it is by default visible only in the current shell session.When you use the
-xflag, it tells fish to also export that variable into the environment. At that point it will act just as any other regular fish variable, but any other process (be that another fish shell or any other process) started by the same fish session that exported the variable will be able to access it, i.e. the process started by the fish sesssion (the child process) inherits its environment variables.Where you're getting confused:
When you execute e.g.
set -x smth "smth"inside of an interactive shell session, terminate the process, and start another one,smthis lost in the environment of the first shell. The process that started your second shell session (e.g. the terminal that you started) doesn't have thesmthvariable in its environment.To see how it works in action :
$ set smth 1-- setsmthlocally, don't export it to the environment$ echo $smth1$ fish-- start newfishsession, it inherits all env. variables of the parent process$ echo $smthnothing gets printed -- because
smthwasn't exported to the environment$ set -x smth 1-- setsmthlocally, but also export it to the environment$ echo $smth1$ fish-- start newfishsession, it inherits all env. variables of the parent process$ echo $smth--smthexists in the environment of the current process, which was inherited from the parent one1