Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set environment variables in fish shell script

Tags:

fish

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?

  • OS: macOS High Sierra 10.13.6
  • fish version: 2.7.1
  • iTerm2: 3.2.0
like image 569
Keke Avatar asked Oct 30 '25 19:10

Keke


1 Answers

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.

like image 59
Jim Lewis Avatar answered Nov 02 '25 16:11

Jim Lewis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!