Keke
Keke

Reputation: 101

How to set environment variables in fish shell script

In my fish shell script 'hoge.fish`, I have a code to set envs.

#!/usr/local/bin/fish
set -x HOGE "hello"

but after I exec this script the env is not set correctly and outputs nothing.

./hoge.fish
echo $HOGE

I've tried these code but none of these worked.

set -gx HOGE "hello"
set -gU HOGE "hello"

how can I fix this?

Upvotes: 4

Views: 4439

Answers (3)

NotTheDr01ds
NotTheDr01ds

Reputation: 20630

While sourceing, as in the original answer is definitely the correct mechanism, a comment from the OP to that answer mentioned that they would still prefer a solution that could be executed as a script.

As long as the variables are exported (set -x) in the script, it's possible (but still not necessarily recommended) to do this by execing into another fish shell inside the script:

#!/usr/bin/env fish
set -gx HOGE hello
exec fish

Executing ./hoge.fish will then have a fish shell with HOGE set as expected.

However, be aware:

  • This will result in two fish shell processes running, one inside the other. The first (parent) is the original fish shell. It will spawn a second (child) process based on the shebang line, which will then be replaced by the third instance from the exec line.

  • You can reduce the number of shells that are running simultaneously by starting the script with exec ./hoge.fish. That results in the shebang script replacing the parent process, and then being replaced by the exec line at the end of the script. However, you will still have run fish's startup twice to achieve what a simple source would have done with zero additional startups.

  • It's also important to realize the environment of the new shell will not necessarily be the same as that of the original shell. In particular, local variables from the original shell will not be present in the exec'd shell.

There are use-cases where these pitfalls are worth execing a new shell, but most of the time a simple source will be preferred.

Upvotes: 1

Omar Alvarado
Omar Alvarado

Reputation: 1734

Consider that if you run that from bash shell it will not export the variables with the -U option because it indicates to export to "fish universe" not outside.

If you stay inside fish's shell you still can do it like this:

#!/usr/local/bin/fish
set -Ux HOGE "hello"

And this is the result:

Welcome to fish, the friendly interactive shell
Type help for instructions on how to use fish
~/trash $ ./hoge.fish
~/tr ash $ echo $HOGE
hello

Remember to keep the first line so fish will interpret it properly.

Upvotes: 0

Jim Lewis
Jim Lewis

Reputation: 45045

When you ran the script, it probably set the environment variable correctly, but only in the process that was created when you ran the script....not in the parent session you ran the script from! When the script exited, the process and its environment were destroyed.

If you want to change the environment variable in your current environment, depending on what interactive shell you're using, you can use a command like source hoge.fish, which will execute the commands in your current session rather than a subprocess, so the environment variable changes will persist.

Upvotes: 6

Related Questions