Writing and Running a Script
A Julia script is just a plain text file ending in .jl containing top-level code executed from start to finish; create one with any text editor — say, temp_convert.jl — and run it from a terminal with julia temp_convert.jl. On Unix-like systems you can also make a script directly executable by adding a shebang line #!/usr/bin/env julia as the very first line and marking the file with chmod +x temp_convert.jl, after which ./temp_convert.jl runs it without typing julia explicitly.
Cricket analogy: It's like a pre-written team sheet handed to the umpire before the toss, executed exactly in the order it's written — a .jl script runs its statements top to bottom exactly as written, the same way a submitted lineup determines batting order without further negotiation.
Functions and Control Flow
Functions are defined either verbosely, with function name(args...) ... end, or as a compact one-liner for simple cases, square(x) = x^2 — both forms support default argument values (greet(name, greeting="Hello") = println(greeting, ", ", name)) and keyword arguments after a semicolon (function plot(x; color="blue", width=1) ... end), and unless an explicit return is written, a function returns whatever its last evaluated expression produces.
Cricket analogy: It's like a captain having both a detailed, written field-placement plan for a tricky over (verbose function) and a quick hand signal for a routine change (one-liner square(x) = x^2) — Julia gives you both a full syntax and a compact shorthand for the same underlying idea, a defined procedure.
Control flow reads close to pseudocode: if condition ... elseif other_condition ... else ... end, for i in 1:10 ... end (where 1:10 is a UnitRange, not a materialized array, so looping over it allocates nothing), and while condition ... end; all three constructs use end to close their block rather than indentation or braces, and break/continue work inside loops exactly as in most other languages.
Cricket analogy: It's like an over being bowled ball by ball through a fixed sequence of six deliveries — for i in 1:6 — where the range itself is just a plan, not six pre-recorded deliveries stored in advance, mirroring how 1:10 in Julia is a lazy range, not a materialized array.
Input, Output, and String Interpolation
println and print write to standard output (println appends a newline, print does not), and string interpolation with $ embeds any expression directly into a string literal — println("Result: $(x + y)") evaluates x + y and splices it into the printed text, with the parentheses required whenever the interpolated expression is more than a bare variable name. Command-line arguments passed to a script land in the global ARGS array of strings, so julia convert.jl 100 makes ARGS[1] equal to the string "100", which typically needs an explicit parse(Float64, ARGS[1]) before it can be used numerically.
Cricket analogy: It's like a stadium's live scoreboard splicing the current partnership total directly into a fixed caption template, 'Partnership: 87 runs' — the way "Result: $(x + y)" splices a computed value directly into printed text via string interpolation.
Putting It Together: A Complete Small Program
A small, complete script typically defines one or more functions, then calls them from a guarded entry point — if abspath(PROGRAM_FILE) == @__FILE__ ... end — a convention borrowed conceptually from Python's if __name__ == "__main__": that ensures the block only runs when the file is executed directly, not when it's include-d as a library from another file; this keeps a script both runnable standalone and reusable as a module of functions.
Cricket analogy: It's like a franchise's youth-team drills being reusable material a senior coach can borrow, but the drills only run as a full session when the youth coach personally calls practice — if abspath(PROGRAM_FILE) == @__FILE__ gates a script's main logic the same way, only running on direct execution.
#!/usr/bin/env julia
# temp_convert.jl -- convert a Fahrenheit temperature to Celsius
function fahrenheit_to_celsius(f::Real)
return (f - 32) * 5 / 9
end
function main()
if length(ARGS) < 1
println("Usage: julia temp_convert.jl <fahrenheit>")
return
end
f = parse(Float64, ARGS[1])
c = fahrenheit_to_celsius(f)
println("$(f) degrees F is $(round(c, digits=1)) degrees C")
end
if abspath(PROGRAM_FILE) == @__FILE__
main()
endWrap timing experiments in the @time macro (@time fahrenheit_to_celsius(98.6)) to see elapsed time, memory allocated, and garbage-collection time for a single call — just remember the very first call includes JIT compilation time, so call the function once to 'warm it up' before trusting the numbers.
The first time you run any script or call any function in a fresh Julia process, expect a noticeable pause — often called 'time-to-first-X' — while Julia compiles the code path being exercised; this is a one-time cost per process, not a sign your script is slow, and it's a major reason long-running Julia processes (like a REPL session or a server) are far more common in practice than always starting fresh.
- A Julia script is a plain
.jltext file run top-to-bottom withjulia script.jl, or made directly executable with a#!/usr/bin/env juliashebang line. - Functions can be written verbosely (
function...end) or as compact one-liners (f(x) = x^2), both supporting default and keyword arguments. if/elseif/else,for, andwhileall close withend, andfor i in 1:10iterates a lazy range rather than a pre-built array.println/printwrite to stdout, and$interpolation ("$(expr)") splices computed values directly into strings.- Command-line arguments arrive as strings in the
ARGSarray and typically needparse(Type, ...)before numeric use. - The
if abspath(PROGRAM_FILE) == @__FILE__ ... endguard lets a file work both as a standalone script and an includable library. - The first run of any code path pays a one-time JIT compilation cost, often called 'time-to-first-X.'
Practice what you learned
1. How do you run a Julia script named `analyze.jl` from the terminal?
2. What is the compact one-liner equivalent of `function square(x); return x^2; end`?
3. In `for i in 1:10 ... end`, what kind of object is `1:10`?
4. What type does `ARGS[1]` have when a script is run as `julia script.jl 42`?
5. What is the purpose of `if abspath(PROGRAM_FILE) == @__FILE__ ... end` in a Julia script?
Was this page helpful?
You May Also Like
Installing Julia and the REPL
How to install Julia with juliaup and navigate the four modes of Julia's interactive REPL for fast, iterative development.
Julia Operators and Expressions
Julia's arithmetic, comparison, and logical operators, the dot-broadcasting syntax, and how the language's expression-oriented design ties it all together.
Julia Variables and Types
How Julia's dynamic variables work, how its type hierarchy of abstract and concrete types is organized, and why type stability drives performance.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics