Why do the "system" and "!" commands not respect my PATH environment variable in Linux?
15 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
MathWorks Support Team
am 18 Feb. 2022
Beantwortet: MathWorks Support Team
am 3 Mär. 2022
I am using RHEL 7.9 which has an outdated version of GCC. I have downloaded a more recent version of GCC (9.2.0) and put the installation folder on my PATH environment variable. Even though I have put the installation folder for the newer version of GCC at the front of my PATH environment variable, it seems that MATLAB is still using my old installation of GCC.
The following output from the "getenv" command shows the PATH environment variable with the install location of the new GCC version at the front.
>> getenv PATH
ans = '/usr/local/opt/gcc-9.2.0/bin:/usr/local/bin:/usr/local/MATLAB/R2021b/bin:/bin:/usr/bin'
This command shows that the new version of GCC is installed and exists at the installation directory:
>> !/usr/local/opt/gcc-9.2.0/bin/gcc --version gcc
(GCC) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software;see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
When I try to use the shell escape command to execute the "which gcc" command, it does not seem to pick up the new version of GCC that I have installed:
>> !which gcc
/bin/gcc
As shown in the following shell escape command, the GCC that MATLAB will target is the old version of GCC and not the new one:
>> !gcc --version gcc
(GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
I have tried this workflow with the "system" command as well, but it seems to produce the same results.
Is there a way to make MATLAB respect my PATH environment variable and pick up the new version of GCC that I have installed.
Akzeptierte Antwort
MathWorks Support Team
am 18 Feb. 2022
When executing the "system" or "!" command, MATLAB will open a shell and load any resource or startup files associated with launching that shell before running the provided command. To further clarify, MATLAB will open a new shell using the command saved by the SHELL environment variable. For example, if you have your SHELL environment variable set to "/bin/sh", then the "system" or "!" command will launch a SH shell and its associated startup files, i.e. ".profile".
Typically, the new shell spawned when executing the "system" or "!" command will inherit the environment variables from the parent shell that MATLAB was opened from. Following this step, the newly spawned shell will run its startup files which may change the environment variables that were passed along from its parent shell. This is most likely the issue that you are experiencing.
There are 3 different workarounds that can be implemented here depending on the workflow that you are interested in pursuing:
WORKAROUND 1:
One way to work around this issue involves using the "-f" flag to force the shell opened in MATLAB to avoid running startup files such as ".profile". The "-f" flag will generally cause the startup file and resources for most shells to be ignored and the existing environment variables will be used. For example, if you execute the following shell script (This assumes that MATLAB can be found on the PATH environment variable, otherwise utilize the full path to MATLAB to start the application):
#!/bin/sh
matlab
export SHELL="/bin/sh -f"
then MATLAB will open a SH shell without running any startup files, which will result in keeping the environment variables set by the parent shell. This should result in the newer version of GCC being found since the environment variables from the parent shell should be conserved.
WORKAROUND 2:
Alternatively, if you are using the "system" function, you can set the PATH to be the system PATH before executing your LINUX command with the "system" function as demonstrated by the following code:
>> myPath = getenv("PATH");
>> system(['export PATH=' myPath ' ; which gcc']);
This will reload the PATH environment variable of the subshell spawned with the "system" function with the system PATH environment variable.
WORKAROUND 3:
Another approach is to set the PATH environment variable in the shell profile that MATLAB ends up opening. For example, if you are using a SH (bourne) shell, then the startup file should be located at
~/.profile
. You may edit or create this file so that it sets the PATH environment variable with the expected value.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!