[General] Adding paths to `PATH` environment variable

Adding paths to `PATH` environment variable

How did I encounter this?

While taking some lectures on `MySQL`, I was faced with accessing `MySQL` via terminal.
First time, I needed to navigate to the folder where `MySQL` is installed in my Windows PC. Efficiency-wise, I have added the path of the folder to `PATH` environment variable. The following is the general concept of adding paths to `PATH`  

Concept

PATH Environment Variable

The PATH is an environment variable that tells the operating system where to look for executable files when you run a command in a terminal or command prompt.

For example, when typing python in the terminal, the system looks through the directories listed in the PATH variable to find the Python executable. If the executable is not in a folder listed in the PATH, we get a "command not found" or similar error.

Why add a Folder to PATH?

When installing software (e.g., MySQL, Python, Java), its executables are usually located in a specific folder. If this folder isn't in the PATH, we'll need to navigate to that folder every time to run the executable.

Adding the folder to the PATH allows to:

  1. Run the software's commands from any directory in the terminal.
  2. Save time and avoid typing long paths.

How does the PATH work?

  1. The PATH is a List:

    • The PATH variable contains a list of directories separated by a delimiter:
      • On Windows: Semicolons (;).
      • On Linux/macOS: Colons (:).
    • When a command is executed, the OS searches for the corresponding executable in each directory listed in PATH, from first to last.
  2. Order Matters:

    • The OS searches directories in the order they appear in PATH.
    • If two directories have an executable with the same name, the one in the directory listed first in PATH is used.

How to add a directory to PATH?

The process of adding a directory to PATH varies by operating system but follows the same principle:

Windows

  • Edit the PATH variable through the System Properties
  • Use semicolons (;) to separate directories.

Linux/macOS

  • Open the terminal and edit the shell configuration file 
    • e.g., .bashrc, .zshrc, or .bash_profile
  • Add a line like this
    • export PATH=$PATH:/path/to/directory
  • Use colons (:) to separate directories.

Example

Suppose I installed a tool in a directory like /usr/local/mytool/bin (Linux/macOS) or C:\Tools\MyTool\bin (Windows).

  • Without adding the directory to PATH:

    • I must navigate to that directory every time to run the tool:
      cd /usr/local/mytool/bin
      ./mytool
      
  • After adding it to PATH:

    • I can simply type from any location in the terminal.
            ./mytool

When to add a directory to PATH?

  • When it's required to access to a command-line tool or software frequently.
  • When the installation instructions recommend it (e.g., Python, Java, Node.js).
Common Directories in PATH
  1. System Directories:
    • Windows: C:\Windows\System32
    • Linux/macOS: /usr/bin, /bin, /usr/local/bin
  2. User-Specific Directories:
    • Linux/macOS: $HOME/.local/bin

Adding software directories to PATH ensures these commands integrate seamlessly with the system.


Comments