Add Entry to PATH Variable on macOS 10 Sierra: for Current Terminal Session, Permanently for Current User and Permanently for All Users (Global)

This post shows how the PATH variable can be set on macOS 10 Sierra for the current terminal session only, permanently for the current user only and permanently for all users on the system.

In this example, the Tomcat startup.sh executable is added to the PATH variable so that is can be run from the terminal without having to use the full path name.

Summary

  1. Add Entry to PATH Variable for the Current Terminal Session Only
  2. Add Permanent Entry to PATH Variable for the Current User Only
  3. Add Permanent Entry to PATH Variable for all Users on the System (Global)

  • 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 macOS 10 Sierra (here 10.12.5):
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

macOS 10 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

macOS 10 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:

macOS 10 Sierra  Terminal - add entry to PATH variable for current session

  • For the current terminal session, Tomcat can now be started by running startup.sh from any location.

macOS 10 Sierra  Terminal - run Tomcat startup.sh - server started successfully

  • This PATH entry will be lost when closing the current terminal or when opening a new terminal window.

  • 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 macOS.
nano ~/.bash_profile

macOS 10 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.

macOS 10 Sierra  Terminal - Use nano to edit user's bash profile and add entry to 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.

macOS 10 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.

  • To permanently add a PATH entry for all users on the macOS system (global), the /etc/paths file is used.
  • Open the file for editing by running:
sudo nano /etc/paths

macOS 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:

macOS 10 Sierra  Terminal - edit /etc/paths file with nano and add entry to 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.

macOS 10 Sierra  Terminal - confirm different user sees new PATH entry in /etc/paths

  • Note: In this example, 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. Refer to 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