Skip to content

Instantly share code, notes, and snippets.

@windyinsc
Created August 17, 2017 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save windyinsc/a454c00b88c305f33d5387c573a2129b to your computer and use it in GitHub Desktop.
Save windyinsc/a454c00b88c305f33d5387c573a2129b to your computer and use it in GitHub Desktop.
What the hell is env variables and how to add to path

Environment Variables

Check out this Super User explanation of environment variables

We sometimes need to add folders containing programs to the PATH-variable to make them executable from the command line.

How to add folders to PATH on Windows 10

Go to Control Panel > System and Security > System and click on Advanced System Settings.

Imgur

In the new windows, go to Advanced then click on Environment Variables at the bottom.

Imgur

In the new window that pops up, select Path in the list and the click Edit.

Imgur

Here you can remove or add different folders to the path. Every folder added here can be accessed from the command line. If there is an executable file in these folders, you can run it from the command line (normally). Click on New to add a new folder of your choice.

Imgur

Sublime example

If I want to add so I can start Sublime from the terminal I can add the folder that Sublime is in on my computer.

C:/Program Files/Sublime Text 3

In this folder I have the program subl.exe. If this folder is in my path I can write subl from the command line and Sublime will open in a new window. You can also open specific files with subl file.txt for example.


How to add folder to path on Mac

You are probably running bash if you are starting your terminal on macOS. If you want to learn more about using bash you can read this guide: bash-guide@GitHub. bash has a configuration-file in your homefolder where you can set the env variables. You can edit this configuration by running this command in the terminal:

nano ~/.bash_profile

You should get a windows that looks something like this. It's and editor in the terminal window. Here you can add your environment variables

Imgur

You save by pressing Ctrl + O and exit by pressing Ctrl + X. The syntax for doing this is:

export PATH=$PATH:/path/to/folder

For example, this folder should already be in your path but it's just an example:

export PATH=$PATH:/usr/local/bin

When you are done you need to reload bash so it recognizes the changes

. ~/.bash_profile

Symlinks for editors

You can also created symbolic links to programs so you can start them from the terminal. You basically create a shortcut from their folders to /user/local/bin which probably already is in your path. Change the name at the end of the command to whatever you want. In these examples you will start sublime with sublime and atom with atom.

Sublime
sudo ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" /usr/local/bin/sublime
Atom
sudo ln -s /Applications/Atom.app/Contents/Resources/app/atom.sh /usr/local/bin/atom

Brackets

sudo ln -s /Applications/Brackets.app/Contents/Resources/brackets.sh /usr/local/bin/Brackets

Fix npm --global errors

If you are getting installation errors when installing npm packages globally on macOS just run this command in the terminal and enter your password:

sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}

It will change permissions for the folders that npm packages are installed in. Source

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