how to chain 2 commands on terminal

I am trying to use subprocess in my python script to open Julia and then run a script.

To run on my machine, I enter this in terminal:

$ julia
$ include(test.jl); func("in.csv", "out.csv")

How do I replicate this process and chain both of these commands so that I can run from subprocess in a single call?

I’ve tried julia; include(test.jl); func("in.csv", "out.csv") and julia && include(test.jl) && func("in.csv", "out.csv")

but both result in

-bash: syntax error near unexpected token `"test.jl"`

Answer

The key here is that you’re not really chaining two commands from the standpoint of Python’s subprocess. There’s just one command: julia. You want to pass a somewhat complicated argument to Julia that will execute multiple Julia expressions.

In short, you just want to do:

subprocess.run(['julia','-e','include("test.jl"); func("in.csv", "out.csv")'])

What’s happening here is that you’re just executing one subprocess, julia, started up with the -e command line flag that just runs whatever comes next in Julia. You can optionally use the capitalized -E flag instead which will print out whatever func (your last expression there) returns.

It’s worth pointing out, though, that there are better ways of getting Julia and Python interoperating — especially if you need to transfer data back and forth.