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

78 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
Oleg Komarov
Oleg Komarov on 28 Jun 2011
I didn't get any result for system('echo $Path') so I was unsure if it was the path used in getenv('path'). Thank you for the explanation Walter.
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
Jason Ross
Jason Ross on 28 Jun 2011
To add on to Walter's answer ... in a UNIX environment (especially an enterprise one) there are generally preferred ways to do this, generally using some sort of file in your home directory. The scripts in /etc will generally call out to a ~/.cshrc_local or ~/.bashrc_local (or something like it) that allows users to set up their own environments without having to edit the system files. The system files will also typically have a comment that tells you NOT to edit them, as they will be wiped in an upgrade.
On Windows, you somehow look at the properties of the computer. This has changed a little in various Windows versions over time, but on Win7 you can select Start > Computer, right-click, then Properties, Advanced System Settings. In the Environment Variables .... button you can edit the PATH for the system (aka All Users) or User.
Be very careful with path edits ... while the path is a very useful tool, it can also be extremely frustrating to have name collisions of a function and get weird results. It's also something that you can't be guaranteed when you move to another system or OS, so if you are writing something that expects the environment to be configured in a portable manner, then you might want to spend some time setting up the environment properly in your code.
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!