12

In macOS Sierra 10.12.x, what is the modern way to add a directory to the PATH environment variable that can work across shells or specifically work with zsh shell (I switched from the default bash to zsh).

I have seen some Questions and Answers such as this one that are bash-specific. And I have seen some such as this one about launchd but it seems I've read launchd is not the right right in later versions of macOS (not sure). I could find no specific references for Sierra, so I am posting now.

I am trying to install Maven with instructions saying:

Add the bin directory of the created directory apache-maven-3.3.9 to the PATH environment variable

1
  • For maven you just need to edit the shell start files in your home directory. The more complex ways are for running guide apps – mmmmmm Mar 6 '17 at 11:22
9

export

To change the path within your shell session, not system-wide, you can use the bash-style export command with zsh, as documented here.

The name of the PATH variable is case-sensitive and must be all-uppercase.

export PATH=$PATH:/folder/you/want

To verify your change, run:

echo $PATH

You will see results like this.

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/folder/you/want

8

This answer (to a different question) suggests modifying /private/etc/paths or adding a file to the directory /private/etc/paths/d. I just tried sudo nano /private/etc/paths, added /foo at the end of the file, and opened a new tab in Terminal, and echo $PATH showed /foo as expected, in both bash and zsh shells.

Assuming you've installed Maven to /opt/apache-maven-3.3.3 you could add /opt/apache-maven-3.3.3/bin to /private/etc/paths. However, and I've not tried this (it's been several years since I last did any Java development), I'd be inclined to instead add a file Maven (containing /opt/apache-maven-3.3.3/bin) to /private/etc/paths.d - that'll encapsulate the Maven-specific path change, making it easier to clean up if you ever delete (or upgrade) Maven.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .