Add Entry to Mac OS X PATH Variable

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

  1. Add Entry to Mac OS X PATH Variable – Current Terminal Session Only
  2. Add Permanent Entry to Mac OS X PATH Variable – Current User Only
  3. 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

  • Open a new terminal window and use the echo command to view the PATH variable.
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

Mac OS X Sierra Terminal - view current PATH variable

  • 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

Mac OS X Sierra  Terminal - run Tomcat 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:

Mac OS X Sierra  Terminal - add entry to Mac OS X PATH variable for current session

  • For the current terminal session, the script startup.sh can now be executed from any location.

Mac OS X Sierra  Terminal - run Tomcat startup.sh - server started successfully

  • 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

Mac OS X Sierra  Terminal - Start nano to edit user bash profile

  • Add the desired PATH entry with e.g. GNU nano as shown below. Use CTRL+O to save changes and CTRL+X to exit.

Mac OS X Sierra  Terminal - Use nano to edit user's bash profile and add entry to Mac OS X PATH variable

  • 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.

Mac OS X Sierra  Terminal - confirm PATH entry was added to current user's bash profile

  • 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

Mac OS Terminal - view /etc/paths file and use nano to open for edit

  • Add the desired PATH entry as shown below. This file maintains a list of PATH entries, one per row:

Mac OS X Sierra  Terminal - edit /etc/paths file with nano and add entry to Mac OS X PATH variable for all users

  • Use CTRL+O to save changes and CTRL+X to exit.
  • Switch to another user on the Mac and confirm that the PATH entry is present.

Mac OS X Sierra  Terminal - confirm different user sees new PATH entry

  • 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.

Leave a Reply