I have a txt file called exec.txt, that I have made executable using chmod. Running it from the shell using ./exec.txt works correctly. However, I need to run it from my Mathematica nb file.

Following is what I tried:

Run[NotebookDirectory[] <> "exec.txt"]

This throws me an output of $0$ in the nb (which is supposedly what I should observe if the code worked properly), but I don't get the desired output from the executable. Both the executable and the nb are in the same directory.

However, opening Mathematica kernel from the shell (after entering the directory of nb and executable) and then trying the following command provides the desired result:

Run[Directory[] <> "/exec.txt"]

What should I do to get the desired result from my nb file?

Details about exec.txt:

It contains just the following line:

tr '{}' '[]' <fileABC.dat | tee fileABC.dat

which simply replaces all curly brackets in the dat file with square ones. This is what I mean while saying "desired result".

  • $\begingroup$ Where do you expect the output to go? $\endgroup$
    – John Doty
    Sep 5, 2021 at 12:09
  • $\begingroup$ @JohnDoty, I expect to observe the same behaviour in the nb as observed in the Mathematica kernel from the shell. The kernel output simply displays the content in the dat file with all curly brackets replaced by square ones and then throws an output of 0. The output of the code in the executable is stored back into the dat file. $\endgroup$
    – abstract
    Sep 5, 2021 at 12:19
  • $\begingroup$ Related: mathematica.stackexchange.com/q/18548/4999 $\endgroup$
    – Michael E2
    Oct 5, 2021 at 13:59

1 Answer 1


To capture the standard output of an external command, use Get with the command preceded by !:

<< "!whoami|tr a-z A-Z"
(* JPD *)
  • $\begingroup$ Thanks for the response. I eventually figured out the solution from the discussion in mathematica.stackexchange.com/questions/109435/… Just added SetDirectory[NotebookDirectory[]] at the top of my nb, and it worked properly. Used Run["./exec.txt"] as the command. $\endgroup$
    – abstract
    Sep 5, 2021 at 13:04

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.