On your Mac, running apps through the terminal is a quick and effective way to do tasks. It provides additional capability and granular control over the program’s operation and output that its GUI equivalent would otherwise lack.
However, using the CLI to launch programs might occasionally result in issues. One such instance is the command not found error that the shell displays when you attempt to run particular applications or commands.
Although you can temporarily get around this by adding the application’s absolute path to your command, this isn’t a particularly practical solution if you need to use that program again.
Setting the PATH variable for this software is another (and more effective) workaround for the issue. Continue reading as we describe the PATH variable and outline the processes for setting it in macOS.
What is the PATH Environment Variable?
All Unix- and Unix-like operating systems use the environment variable PATH, sometimes known as PATH variable. The path, the locations of system programs or processes, and other crucial information needed by other system programs are only a few examples of the name-value pairs that make up environment variables for various programs or processes on an operating system.
Regarding PATH, the variable holds a list of all the directories (for various applications added to PATH) that the shell must look for in order to run the programs you provide through a terminal command.
Why Do You Need to Set the PATH Environment Variable?
When you run a command in the terminal on macOS, the PATH environment variable is searched for the path of the requested programme. If a path address is located, the command is successfully carried out, and the output is returned. If not, you encounter the error “command not found.”
As we indicated before, one approach to fix this mistake is to include the absolute or full path to the location where the software is installed or saved in your command. Unfortunately, this isn’t a very effective method and can’t be utilised when you want to perform instructions repeatedly because doing so regularly would require a lot of your time and work.
On the other hand, if the PATH variable is set with the program’s path, you can use the programme without giving its absolute path in any directory on your system.
Basically, this indicates that you should run your command as follows rather than as follows:
You can easily employ:
any directory on the file system, anywhere.
How to Set the PATH Variable in macOS
Unlike Windows, which allows you to set the PATH variable using both the GUI and CLI, macOS only supports setting it via the CLI. Additionally, there are two ways to set the PATH on your Mac: temporary and permanent, depending on your needs.
When you temporarily alter the PATH, your modifications only take effect during the current session; once the session is over, the PATH returns to its original setting. On the other hand, setting the PATH permanently will keep your modifications and apply them to all of your sessions—even when you open a new terminal window or restart your Mac.
Now that it is out of the way, proceed to the sections below for instructions on how to establish PATH on your Mac.
Identifying the Current PATH Entries
You should first check the present entries in your system’s PATH to make sure there isn’t already an entry for the program’s path there before adding it to the PATH variable on your Mac.
Open the Terminal program and enter the following command to see the current PATH settings.
environment variable output for PATH
You can skip this step if you’re configuring PATH for a new program or script.
Setting the PATH Variable Temporarily
You can now set the PATH for any program after you have determined the current PATH entries. You can temporarily set a program’s path with the following command if you only want to use/run it in your current terminal session:
For instance, you would execute the following command to configure PATH for Python 3.6:
/Library/Frameworks/Python.framework/Versions/3.6/bin export PATH=$PATH
By doing this, you’ll provide the program a temporary variable that you can use in the commands you type in the active terminal window or terminal session.
Setting the PATH Variable Permanently
A program’s route must be permanently configured if you plan to use it frequently. You must add the program’s path to the shell’s configuration or profile file in order to accomplish this.
This can be done using either the bash shell or the zsh shell, depending on the version of macOS that your Mac is running (z shell).
Bash (.bashrc or.bash profile) is appropriate for earlier versions of macOS (pre-Catalina).
Zsh (.zshrc or.zsh profile) is available for macOS Catalina and later.
Open the terminal and type the following command based on your shell now that you are aware of the file where you need to put the path for your program or script:
/.bash profile nano
.bashrc and nano
/.zsh profile in nano
/.zshrc in nano
This program will generate a new configuration or profile file if one is absent from your system; if one already exists, it will open it so you can update it.
The only thing left to do is locate the application or script’s whole path on the file system. To do this, launch the Finder and go to the directory that contains the program’s or script’s installation or storage location. Click on the program or script in question, hold down the Option key, and choose Copy xyz as Pathname, where xyz is the name of the program.
ALSO ON TECHPP commands for the macOS terminal
The Most Important macOS Terminal Commands [Guide]
Return to the terminal and use nano or another text editor to open the relevant configuration file or shell profile. Put the next line in the document:
where path/to/directory must be changed to the precise path you copied in the previous step.
Alternately, if the file already has path entries, add the following line after the colon (:):
To write your modifications to the file, press Control + O. Press Return to continue with the default when asked to confirm the file name. For the editor’s exit key, use Control + X.
Now, open a terminal window and enter the following commands to see if the path has been added:
Finally, to begin a fresh session, dismiss the terminal window and reopen it. Alternately, you can issue the subsequent command to make the modifications take effect right away:
.bash profile source
/.zsh profile source
You ought to be able to launch your application or script from any directory in the file system after that is finished.
Add to PATH Mac: Accessing Programs From Anywhere via Terminal
You can now execute or access the application you wish to use from anywhere in the file system using the terminal after setting the PATH environment variable to use that path. Setting the PATH for these scripts can make your life easier if you use Python or shell scripts to automate your workflow because you can now access them from any directory without having to specify their absolute paths.
FAQs About Setting PATH on macOS
1. How do I set an environment variable in Catalina Mac?
If you’re using macOS Catalina (or higher), establishing the environment variable is as easy as adding the path of the script or application you want to launch anywhere in the shell configuration or profile file. Since Catalina (and later) versions of macOS use zsh as the default shell, you only need to change the.zshrc or.zsh profile file and add the path to your script or program. To learn the steps and various approaches, refer back to the earlier sections of the text.
2. How do I permanently set PATH on Mac?
If you set the PATH environment variable on a Mac permanently, unlike with temporary variables, your PATH environment variable modifications are not restricted to the current shell session. So even if you start a new session or restart your Mac, your system’s shell may still access it. Simply open either bash files (.zshrc or.zsh profile) or zsh files (.zshrc or.zsh profile) and add your program’s or script’s PATH to them to permanently set PATH on a Mac. Which are detailed in the above instructions as steps to take.
3. How do I find the PATH variable on a Mac?
On a Mac, launch a terminal window and type echo $PATH to locate the PATH variable. A list of all the directories currently specified under the PATH environment variable on your Mac will then be returned by the shell.