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

>> 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 Commenti

What path are you talking about?
Oleg, PATH is the environment variable that determines which directories are searched for executables (and which order they are searched in.)
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.
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.
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.

Accedi per commentare.

Risposte (2)

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 Commento

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

Accedi per commentare.

Walter Roberson
Walter Roberson il 28 Giu 2011

0 voti

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 Commenti

Thanks! How complicated is the permanent change?
On Unix-type systems, look at /etc/profile as that will likely lead you to the best place to make the change. For example on OS-X it will lead you to the path_helper utility which will lead you to /etc/paths and the /etc/paths.d directory
On MS Windows... Hmmm, I've forgotten now, but I remember the places I would look if I should need to do it.
Jan
Jan il 28 Giu 2011
I'm adding folder to my PATH in the .bashrc script. This is not complicated, but work only, if the .bashrc had run. But because this does not concern Matlab, better ask Google for more details.
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.
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.
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.

Accedi per commentare.

Categorie

Prodotti

Richiesto:

il 28 Giu 2011

Community Treasure Hunt

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

Start Hunting!

Translated by