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

47 views (last 30 days)
Randall Lin
Randall Lin on 28 Jun 2011
Commented: Walter Roberson on 4 Feb 2026 at 20:21
>> 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!
  5 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.
Joshua Carmichael
Joshua Carmichael on 4 Feb 2026 at 19:23
I have the same problem. My !echo $PATH does not match what echo $PATH outputs from my terminal. My shell is zsh. I did the following. It seemed to work and may be an easy solution (I'll paste this answer to similar questions on the Community too). First, open Matlab (not terminal).
text = fileread('~/.zprofile');
str1 = split(text,{':'}); %path additions separate by colons.
str1 = cellfun(@deblank, str1, 'UniformOutput', false);%omit newlines, etc...
myfun = @(s) sprintf('addpath(%s)', str1{s});
str2 = string(arrayfun(myfun, 1:length(str1),'UniformOutput',false)');
Now input str2 into startup.m or another function. I have something called addhdpaths.m that adds paths from a hard drive and I included in there too.

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()
  6 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.
Walter Roberson
Walter Roberson on 4 Feb 2026 at 20:21
2026 update:
The proper way to update PATH system-wide has changed a couple of times on MacOS.
For a while it involved /etc/paths and /etc/paths.d .
After that it involved editing a specific plist file that was imported by the daemon process.
Starting roughly Catalina, that plist file stopped working, and it is difficult to get a straight answer as to how to affect the system-wide PATH. As best I can tell, these days you are intended to edit plist associated with each individual application. I experimented trying to get it to work, but I was not able to get it to function.

Sign in to comment.

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!