I am trying to run bash commands from within Mathematica. Funnily enough, Mathematica seems to use a different $PATH environment than the system environment. If I try


or equivalently in Mathematica 10

RunProcess[$SystemShell, All, "echo $PATH"]

I get (in StandardOutput):


However, if I run echo $PATH on my (OS X) terminal, I get (both as user and root):


which is indeed the full path. Why doesn't use Mathematica the same environmental variable? Is this expected behaviour?


I already mentioned in the comments why this question is different from this one, but to avoid getting this question marked as a duplicate I'll repeat it here.

That thread does not deal with system environment variables (it's an unfortunate wording in the topic title) but with Mathematica environment variables. The path variable for the latter is stored in $Path in Mathematica, while the former can be accessed by executing Environment["PATH"] (which gives the OS system environment path). I'm really asking about the system environment variable here, but thanks to ilian and m_goldberg I realise now that it is an OS X issue.

  • $\begingroup$ @dionys I've seen that one as well, but that thread does not deal with system environment variables (typo in the topic) but Mma environment variables. See the difference between $Path and 'PATH'in Mma. I'm really asking about the system environment variable here, which you access with Environment["PATH"]. $\endgroup$ – freddieknets Nov 17 '15 at 16:33
  • $\begingroup$ @dionys When I run a bash command inside Mma using Run[] or RunProcess[], it will only recognise commands inside the system wide $PATH variable (Unix notation). What I don't understand is why it doesn't use my system path, but seems to use some default unix path which is only part of it. $\endgroup$ – freddieknets Nov 17 '15 at 16:38
  • $\begingroup$ @dionys No problem, thanks for your comment :-). $\endgroup$ – freddieknets Nov 17 '15 at 16:38
  • $\begingroup$ The docs for Environment indicate the path is system dependent ... perhaps your shell sets a user-specific environment that differs from the system default? RunProcess also has a ProcessEnvironment option you can take advantage of. $\endgroup$ – dionys Nov 17 '15 at 16:44
  • $\begingroup$ I'm having this problem in Windows mathematica $\endgroup$ – Rainb Jul 8 at 7:18

It is certainly an OS X issue. Assuming the default shell is configured, when Terminal starts up it runs the script file ~/.bash_profile, which usually modifies the PATH environment variable. You can do the same thing in Mathematica with

SetEnvironment["PATH" -> Import["!source ~/.bash_profile; echo $PATH", "Text"]]

After which


should match what you see in Terminal.

| improve this answer | |
  • $\begingroup$ It still doesn't match up fully. After sourcing .bash_profile, most paths in /opt, /usr, /bin and /sbin are present, but the paths in /opt/X11, /Library/TeX and /usr/texbin are still missing. So probably OS X has another source as well for its $PATH variable, but I guess it's no longer a question for Mathematica.SE anymore then.. $\endgroup$ – freddieknets Nov 17 '15 at 22:39
  • $\begingroup$ @freddieknets the other OS X source for $PATH is /etc/path and the files in /etc/paths.d. They are loaded by the OS X utility path_helper. $\endgroup$ – Virgil Jan 27 '16 at 16:59
  • $\begingroup$ This worked... but then didn't. Nonetheless up vote :) $\endgroup$ – SumNeuron Jun 23 '17 at 19:12
  • $\begingroup$ Mathematica 11.2 under macOS Sierra (10.12.6) is not getting its value of Environment["PATH"] from /etc/path and the files in /etc/paths.d. In fact, the output from Environment["PATH"] is only "/usr/bin:/bin:/usr/sbin:/sbin"; however, /etc/path includes the additional path /usr/local/bin, and on my system /etc/paths.d includes among others the file TeX containing the additional path /Library/TeX/texbin. $\endgroup$ – murray Sep 24 '17 at 14:46

I think this is an OS X issue. Note that if you launch Mathematica from a terminal, you will get the value of Environment["PATH"] which you expected.

For GUI applications that are started through Finder/LaunchServices, the environment (and $PATH in particular) is different and determined by the OS (this is a related recent question that had to do with setting up umask).

This behavior may be changed, though the exact mechanism seems to evolve with each OS X release and I don't know off the top of my head what is the best approach to achieve this on Yosemite or El Capitan.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.