MATLAB: MATLAB started as an app in OS X causes problems with “unix” function

macMATLABpathunix

I noticed that the function "unix" acts differently, mainly in regards to my PATH variable, depending on whether I start MATLAB from the application or from Terminal.
When I start from Terminal, I get the same $PATH that I would in that Terminal window:
>> unix('echo $PATH')
/usr/local/fsl/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/Applications/Inkscape.app/Contents/Resources/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin
When I start from the Application, it gets a very basic PATH from… somewhere:
>> unix('echo $PATH')
/usr/bin:/bin:/usr/sbin:/sbin
I can change my path, but it reverts the next time I use the unix function:
>> unix('source /Users/pcrutchl/.bash_profile; echo $PATH')
/usr/local/fsl/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/Applications/Inkscape.app/Contents/Resources/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin
ans =
0
>> unix('echo $PATH')
/usr/bin:/bin:/usr/sbin:/sbin
ans =
0
(To be clear: changing the path manually every time, or even initially, isn't really a solution, just an exploration of the behavior. If I could put something in startup.m to fix it, that would be fine.)
Is this known behavior? Is there a good way to get the application MATLAB to behave like the MATLAB launched from command line?

Best Answer

This is standard UNIX behavior, not the responsibility of MATLAB.
Each time you unix(), a new shell process is created. You do whatever to the environment variables of that shell process, but then that shell process ends before control returns to MATLAB.
You can setenv() from within MATLAB to affect the environment variables that are passed to all unix() shells open after the setenv (in that MATLAB session)
Or you can use the normal configuration files of your shell to tell it that it should execute appropriate commands each time the shell is started.
I suggest that in a terminal shell, you execute
man bash
skip the options section for now and pay serious attention starting near "The following paragraphs describe how bash executes its startup files."
Related Question