# How to run multiple scripts in a row from command line using the same kernel?

Here is my shell script so far (I am using MacOSX):

#!/bin/sh
math="/Applications/Mathematica.app/Contents/MacOS/MathKernel"
$math -run "<<input" -script script1.wl script2.wl > output  The two scripts I want to run in a row are script1.wl and script2.wl. The above seems to only run script1.wl Anything obvious I am missing ? • I've never used the -script option. What happens if you simply use $math -run "<<input;<<script1.wl;<< script2.wl" > output? – Jason B. Mar 15 '17 at 16:32
• If you want to run them with the same kernel, why don't you run them from within Mathematica (Get) instead of from within a shell script? – Szabolcs Mar 15 '17 at 16:43
• @Szabolcs - sometimes you just don't want to be burdened with a front end :-) – Jason B. Mar 15 '17 at 16:54
• @JasonB. I am not talking about the front end. I just don't think that it is a good idea to mix Mathematica will shell scripts. I meant: write Mathematica code to read in the two "scripts", not shell code. – Szabolcs Mar 15 '17 at 16:56
• @JasonB. That's essentially the same what you said. Sorry. I didn't red carefully. – Szabolcs Mar 15 '17 at 16:58

I can't see any syntax on this page for using 2 files with the -script option (and as MB1965 points out, the help does say that only one script file can be used with the -script option). I do have some experience running WL files from the command line in Mathematica in batch mode, all of which I learned from Jens's page here.

If I create a file called "script1.wl" with the contents

x1 = 3;
x2 = 4;


and another file in the same directory, "script2.wl", with

Print[x1 + x2];
Exit[]


and then run this command from the same directory

math -noprompt -run "<<script1.wl;<<script2.wl" > output


where I've set math up as an alias in my shell to the MathKernel application, then the output file has a single line

7

• Similarly if you run RunProcess[{\$script, "-help"}, "StandardOutput"] it specifies that only one file can be passed. – b3m2a1 Mar 15 '17 at 17:18