How do I add to the PATH of system()?
83 ビュー (過去 30 日間)
古いコメントを表示
>> 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 件のコメント
Oleg Komarov
2011 年 6 月 28 日
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
2011 年 6 月 28 日
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.
回答 (2 件)
Jan Pospisil
2020 年 3 月 1 日
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 件のコメント
Brian Derstine
2021 年 8 月 18 日
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
Walter Roberson
2011 年 6 月 28 日
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 件のコメント
Jason Ross
2011 年 6 月 28 日
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
2011 年 6 月 28 日
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.
参考
カテゴリ
Help Center および File Exchange で Naming Conventions についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!