MATLAB Answers

How do I add to the PATH of system()?

188 views (last 30 days)
Randall Lin
Randall Lin on 28 Jun 2011
Commented: Brian Derstine on 18 Aug 2021
>> system('echo $PATH')
returns
/usr/bin:/bin:/usr/sbin:/sbin
Can I add to this path? I tried the usual commands for changing PATH through system('command'), but these did not work? Help? Thanks!
  4 Comments
Walter Roberson
Walter Roberson on 28 Jun 2011
The names of environment variables are usually case-sensitive, at least on Unix systems. On Unix systems, traditionally path (lowercase) is used in csh-derived shells for a path variable that is local to the shell process itself, and traditionally PATH (uppercase) is used in sh-derived shells, and also used in csh-derived shells of a path variable that will be "exported" (set for child processes). Traditionally.

Sign in to comment.

Answers (2)

Jan Pospisil
Jan Pospisil on 1 Mar 2020
Since some of the other comments here are not detailed, I post a new answer clarifying how I understand the problem. Although the question is from 2011, it is still surprisingly valid in 2020, because in default installations (at least on Mac) there is a difference between
>> system('echo $PATH')
and
>> getenv('PATH')
ans =
'/usr/bin:/bin:/usr/sbin:/sbin'
To fix it, you may put into your startup.m e.g. the following:
% update PATH
[~,result]=system('echo -n $PATH');
setenv('PATH',result)
clear result
  1 Comment
Brian Derstine
Brian Derstine on 18 Aug 2021
This was helpful. The problem for me was not that there was a difference between system('echo $PATH') and getenv('PATH'). On the contrary, these were exactly the same. The problem was a difference between system('echo $PATH') and running echo $PATH from Terminal (on Mac). For whatever reason, Matlab was missing a folder that was in my Terminal PATH. I was able to edit my startup.m file to manually add the path I needed like:
if ismac
while lfscheck
[~, lfsloc] = system('git-lfs --version');
% check if git-lfs is installed
gitLfsStatus = regexp(lfsloc, 'git-lfs/', 'once');
% git-lfs not installed
if isempty(gitLfsStatus)
warning(sprintf(['Could not find git-lfs installed for command line.\n'...
'Fixing path... ']))
[~, result] = system('echo -n $PATH');
% add /usr/local/bin to the PATH (this is where git-lfs installs)
result = [result ':/usr/local/bin'];
setenv('PATH', result);
getenv('PATH') % verify that PATH was updated
clear result;
else
lfscheck = false;
end
end
end

Sign in to comment.


Walter Roberson
Walter Roberson on 28 Jun 2011
setenv() can be used to change PATH for that process and all subprocesses. Changing PATH permanently is more complex, so sometimes the easiest approach is to modify your startup.m to do the setenv()
  5 Comments
Walter Roberson
Walter Roberson on 28 Jun 2011
System properties... yes, that sounds right for MS Windows.
The proper place on a Unix system can depend upon which shell the user logs in with. The man pages for the individual shells describe the sequence for that shell.

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!