4
$\begingroup$

I would like to run external command from Mathematica. For this I need to have bash environment configured via reading the content of ${HOME}/.bashrc file. When I execute the following code in Mathematica notebook

ReadList["!echo $PATH", String]

the content of PATH variable is not set as it is in ${HOME}/.bashrc. The same happens when I explicitly load bash configuration file

ReadList["!source ${HOME}/.bashrc; echo $PATH", String]

(strictly speaking the above command makes no effect!). So the question is: is it possible to load the configuration stored in .bashrc file in Mathematica external call under the Linux system (of course without explicitly setting e.g. mentioned PATH variable is not a solution for me)?

$\endgroup$
5
  • $\begingroup$ try ReadList["!printenv",String] start by figuring aout what shell is running and what environment is avaialable. $\endgroup$
    – george2079
    Commented Dec 28, 2013 at 13:45
  • $\begingroup$ Why not use Run? $\endgroup$ Commented Dec 28, 2013 at 22:50
  • $\begingroup$ Played with this a bit: you can reveal the actual shell with this: Readlist["!/bin/ps -p $$"] -- In my case I've got the bourne shell /bin/sh. printenv incorrectly shows the SHELL variable inherited from the shell used to invoke mathematica. So.. make shure your bashrc doesnt do anything incompatable with plain /bin/sh .. $\endgroup$
    – george2079
    Commented Dec 30, 2013 at 19:18
  • $\begingroup$ I don't understand the question. ReadList["!echo $PATH", String] gives me {"/usr/bin:/bin:/usr/sbin:/sbin"} which is expected since it did not read the contents of my bashrc file. If I do ReadList["!source $HOME/.bash_profile; echo $PATH", String] then I get all of the path that I have set up in there. $\endgroup$
    – jmlopez
    Commented Jan 2, 2014 at 1:43
  • $\begingroup$ As I have stated in the question ReadList["!source $HOME/.bashrc; echo $PATH", String] does not take any effect for me. $\endgroup$
    – mmal
    Commented Mar 9, 2014 at 11:36

1 Answer 1

1
$\begingroup$

The solution for me is to start the Mathematica form a terminal, instead of Gnome launcher. Then the bash environment, when ReadList is executed, will be inherited from the shell itself.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.