I am trying to run gnuplot and send it some plotting commands from Mathematica. Unfortunately without any luck. For me the documentation is completely useless as it gives little to no practical examples. My attempts look like this:

RunThrough["gnuplot", "set term png\n set out \"test.png\"\n test"]

Now this produces no output in Mathematica and neither the test file is produced. So I tried

<< "!echo $TERM"

which is in documentation here. This again produces nothing and by nothing I mean absolutely no output:

enter image description here

Finally I tried


as in documentation and it returned: enter image description here.

as expected however the very first execution took almost 10 seconds.

However, I would really like to use RunThrough and I have no idea why it is not doing at least what is described in the documentation. Any ideas what I am doing wrong? (MA 11.1.0 on Debian 9.2).

  • $\begingroup$ The specifics of external calls may depend on your OS. What's yours? Also take care using << as it will evaluate what it gets back from the external call. On my Windows PC << "!echo 1+1" yields 2. $\endgroup$ Nov 11, 2017 at 16:17
  • $\begingroup$ Have you tried RunProcess more than once? Is it slow on second try as well? I could imagine that it is not. If it is not as slow as you report (which is not the case for me) I think it would be the best choice for what you want. RunThrough "reads the output of the program as Wolfram Language Input" which is almost certainly not what you want. If `` then I would use Import as indicated by Henrik... $\endgroup$ Nov 11, 2017 at 17:54

2 Answers 2


This works for me under Linux:

gpcmds = "set term png\nset out \"test.png\"\ntest\nexit";
RunProcess["gnuplot", All, gpcmds]

The documentation for RunThrough says that it passes the InputForm of its second argument to the program. The input form of a string has double quotes, so RunThrough passes the double quotes to gnuplot, which is not what we want. As evidence of this behavior, consider writing the output of gnuplot to a file (named tmp999.txt in this example) like this

RunThrough["gnuplot > tmp999.txt 2>&1", "print \"1 + 1\""]

The "2>&1" is important, since gnuplot writes to unit 2. We hope to get back "1 + 1" from gnuplot, so MMA can evaluate it, but instead we get this error message (in our tmp999.txt file) from gnuplot complaining about the double quotes:

RunProcess[{"cat", "tmp999.txt"}]
    <|ExitCode -> 0, StandardOutput -> 
      gnuplot> "print \"1 + 1\"\n"
               line 0: invalid command

      , StandardError -> |>

The lesson seems to be, do not use RunThrough for gnuplot, unless we can find a way to not pass those double quotes to gnuplot.


On macos, I made good experiences by calling command line tools by Import; virtually all other methods don't work for me (well, they did not when I tried the last time). Here an example

Import["! date", "Text"]

"Sat Nov 11 17:49:02 CET 2017"

If the process generates large tables, then "Table" instead of "Text" is a better alternative.

Well, that did also not always help. Once a collegue, a real bash connaisseur, explained me why the shell that Mathematica uses does not call my /.profile and so does not know my path variables. He taught me that either

I have to use the full path to the program to run or

I have to force Mathematica's shell to load my .profile with

Import[StringJoin["! source ", 
  FileNameJoin[{$HomeDirectory, ".profile "}], " && ", 
  programstring], "Text"]

where programstring constains the command as you would type it into the command line.

Hope this helps.

  • $\begingroup$ 2>&1 redirects stderr to stdout, which will Import catch error messages along with the standard output (if you want that). The trailing & runs the program in background, which doesn't really make sense (or a difference) here AFAIK. $\endgroup$ Nov 11, 2017 at 18:02
  • 1
    $\begingroup$ @AlbertRetey Good point. I removed it. $\endgroup$ Nov 11, 2017 at 18:07

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.