iCloud Terminal
If you’ve ever run a command from the terminal without typing its full path, you’ve benefited from the $PATH environment variable. This powerful feature tells your system where to look for executable files, and scripts. Learning how to add to the $PATH can streamline your workflow, making command execution quicker and more efficient.

What is the $PATH?

The $PATH is an environment variable in Unix-like operating systems that lists directories the system is to search to find executable files. By adding directories to your $PATH, you allow the system to recognize custom scripts or programs, enabling you to run them from anywhere in the terminal, using only their filename.

Determining Your Shell

Before modifying the $PATH, you should identify which SHELL you’re using. Open your terminal and type:

echo $SHELL

This will display your current SHELL, such as bash or zsh, which determines the configuration file you’ll need to edit.

Permanently Add to the $PATH

To make changes that persist across sessions:

  1. Locate Your Shell’s Configuration File: Depending on your shell, this could be .zshrc, .bashrc, etc…
  2. Edit the Configuration File: Open it with a text editor, or edit it directly in the terminal, replacing the file with the path to your own configuration file:
nano ~/.zshrc

3. Add the Directory: Insert the following line, replacing /path/to/directory with your desired directory:

export PATH="/path/to/directory:$PATH"

4. Save and Exit: Press Ctrl+X, click the Y key, and ENTER to save and close the file; if you’re using the nano command to edit.

5. Apply the Changes: Either restart your terminal, open a new tab, or run this command replacing the file with the path to your own configuration file:

source ~/.zshrc

Verifying Your Changes

To confirm the updated $PATH has been applied, run:

echo $PATH

You should see the newly added directory listed among other paths.

Temporarily Add to the $PATH

For temporary changes (lasting only for the current session):

export PATH="/path/to/directory:$PATH"

This won’t affect new terminal windows or future sessions, making it ideal for quick, non-permanent setups.

Conclusion

Mastering how to add to the $PATH enhances your command-line efficiency, giving you seamless access to your scripts and tools.

I hope this article provided you with what you were looking for. If you have any feedback, please leave a comment.

Credits

  • Apple – Parts of the Featured Image.