This post shows how to add an entry to the Mac OS X PATH variable – for the current terminal session only, permanently for the current user only and permanently for all users on a Mac OS X system.
In this example, the Tomcat startup.sh
executable is added to the Mac OS X Path Variable so that is can be run from the terminal without having to use the full path name.
Summary
- Add Entry to Mac OS X PATH Variable – Current Terminal Session Only
- Add Permanent Entry to Mac OS X PATH Variable – Current User Only
- Add Permanent Entry to Mac OS X PATH Variable – All Users on the System (Global)
1 Add Entry to Mac OS X PATH Variable – Current Terminal Session Only
echo $PATH
- By default, the variable should be set to something like this on Mac OS X Sierra (here 10.12.5):
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
- On my system, the Tomcat home is located at
/Applications/apache-tomcat-9.0.0.M17
- Staring the server using the
startup.sh
, requires using the full path name, otherwise it is not found.
sudo startup.sh
- To add the Tomcat
bin
folder to the PATH variable for the current terminal session only, execute:
PATH=$PATH:/Applications/apache-tomcat-9.0.0.M17/bin
- Use
echo
again to confirm the new entry to PATH:
- For the current terminal session, the script
startup.sh
can now be executed from any location.
- This PATH entry will be lost when closing the current terminal and is not available in a new terminal.
2 Add Permanent Entry to Mac OS X PATH Variable – Current User Only
- To permanently add a PATH entry for the current user, navigate to the
home
folder:
cd $home
- And execute the below command to edit the user’s bash profile. Bash is the default shell on Mac OS.
nano ~/.bash_profile
- Add the desired PATH entry with e.g. GNU nano as shown below. Use
CTRL+O
to save changes andCTRL+X
to exit.
- For the changes to take effect, close the current terminal window and open a new one.
- Use
echo $PATH
again to confirm the entry was added.
- Whenever a terminal is opened by the current user, the bash profile will add the specified PATH entry.
3 Add Permanent Entry to Mac OS X PATH Variable – All Users on the System (Global)
- To permanently add a PATH entry for all users on Mac OS (global), the
/etc/paths
file is used. - Open the file for editing by running:
sudo nano /etc/paths
- Add the desired PATH entry as shown below. This file maintains a list of PATH entries, one per row:
- Use
CTRL+O
to save changes andCTRL+X
to exit. - Switch to another user on the Mac and confirm that the PATH entry is present.
- Note: Here, the user edgar will only be able to actually run the Tomcat startup.sh if the permissions have been set correctly by the user that owns the files. See chmod for setting permissions.
- If read and execute permissions have not been set, errors indicating that the file was not found and other permission errors will occur.
- Please register to leave a comment if you know additional or better approaches.