Skip to content

Instantly share code, notes, and snippets.

@iest
Last active August 11, 2022 09:20
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save iest/58692bf1001b0424c257 to your computer and use it in GitHub Desktop.
Save iest/58692bf1001b0424c257 to your computer and use it in GitHub Desktop.
Setting up environment variables with various shells

What the hell are environment variables?

They're just variables you set on your system that various programs/processes can read. A fairly standard example in javascript circles would be setting your NODE_ENV variable to "production" or "development", altering how node code is executed on your system (for example showing more debug messaging when in development).

With most shells there's a way to set them for the current session, and a way to set them for all sessions. The following is meant to be a guide on how to set env vars in the various shells.

Bash (The default shell on OSX)

Setting for the session:

$ export MY_VAR="some value"
$ echo $MY_VAR
some value

To persist this, you need to edit the .bash_profile file inside your home directory to include the following line:

# other stuff
export MY_VAR="some value"

You then need to make sure your system loads in the new variables. You can do this using the source command:

$ echo $MY_VAR
->
$ source ~/.bash_profile
$ echo $MY_VAR
-> some value

zsh

Pretty much exactly the same as above, but you'll want to edit your .zshrc file.

fish shell

Fish has quite different syntax to bash. The following is equivalent to the above bash example:

$ set -x SECRET "butts"
$ echo $SECRET
-> butts

To persist the env var, edit ~/.config/fish/config.fish to include the following line:

set -x SECRET "butts"

(the -x option exports the variable so it can be used by programs other than the shell).

@idleberg
Copy link

idleberg commented May 6, 2020

Alternatively, you can use Fish universal variables to persist the env vars

Universal variables are variables that are shared between all the users' fish sessions on the computer. Fish stores many of its configuration options as universal variables. This means that in order to change fish settings, all you have to do is change the variable value once, and it will be automatically updated for all sessions, and preserved across computer reboots and login/logout.

Example:

set -Ux SECRET "butts"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment