how to set environment variables in fish shell

后端 未结 4 1252
一向
一向 2021-01-30 07:59

Can someone please tell me what\'s the correct way to set a bunch of environment variables in the fish shell?

In my .config/fish/config.fish file, I have a function to s

相关标签:
4条回答
  • 2021-01-30 08:23

    Environment Variables in Fish

    I would like to add that, while @JosEduSol's answer is not incorrect and does help solve the OP problem, -g is only setting the scope to be global, while -x is causing the specified environment variable to be exported to child processes.

    The reason the above fails, is because @cfpete is setting the env vars inside a function and the default scope will be local to that function.

    0 讨论(0)
  • 2021-01-30 08:32

    The variables you are declaring are keep in a local scope inside your function.

    Use:

    set -g -x
    

    Here "g" is for global.

    0 讨论(0)
  • 2021-01-30 08:35

    Use Universal Variables

    If the variable has to be shared between all the current user fish instances on the current computer and preserved across restarts of the shell you have to use -U or --universal:

    set -Ux FOO bar
    

    Using set with -g or --global doesn't set the variable persistently between shell instances


    Note:

    Do not append to universal variables in config.fish file, because these variables will then get longer with each new shell instance. Instead, simply run set -Ux once at the command line. And it will be stored in the file .config/fish/fishd.MACHINE_ID, where MACHINE_ID is typically your MAC address.

    0 讨论(0)
  • 2021-01-30 08:44

    another option is to run:

    export (cat env_file.txt |xargs -L 1)
    

    where env_file.txt contains rows of the format VAR=VALUE

    this has the benefit of keeping the variables in a format supported by other shells and tools

    0 讨论(0)
提交回复
热议问题