Non interactive execution
While one of the strength of Julia is its interactive REPL, sometimes you want to run Julia code non-interactively. In this section, we will see the various options to do so.
Sourcing files
Julia scripts have a .jl extension.
The include function sources a Julia script (in a REPL session or in another script):
include("file.jl")The code contained in file.jl is thus run non interactively.
Running code from the terminal
You can run scripts by passing them to the julia command on the command line:
$ julia script.jlThis code is run in a terminal, not in Julia, as is indicated by the $ prompt.
You can also evaluate single expressions in Julia from the command line by using the flag -e:
$ julia -e 'println(2 + 3)'5
Passing arguments
To the julia command itself
If you want to pass arguments to the julia command itself, you need to add them before the script or the Julia expression.
Example:
$ julia -O script.jlTo the script/Julia expression
To pass arguments to the script (or Julia expression if you use -e), you add them after the script or expression:
$ julia script.jl arg1 arg2 arg3arg1, arg2, arg3 will be passed in the global constant ARGS and interpreted as arguments to the script.
Example passing arguments to an expression:
$ julia -e 'for x in ARGS; println(x); end' 2 32
3
To both
To pass arguments both to the julia command and to the script/expression, you need to add the -- delimiter before the script/expression:
$ julia [switches] -- [programfile] [args...]Example:
$ julia -O -- script.jl arg1 arg2