100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Your First Julia Script

How to write, structure, and run a complete Julia script — from functions and control flow to I/O and the standalone-vs-library entry-point convention.

FoundationsBeginner10 min readJul 10, 2026
Analogies

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.

julia
#!/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()
end

Wrap 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 .jl text file run top-to-bottom with julia script.jl, or made directly executable with a #!/usr/bin/env julia shebang 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, and while all close with end, and for i in 1:10 iterates a lazy range rather than a pre-built array.
  • println/print write to stdout, and $ interpolation ("$(expr)") splices computed values directly into strings.
  • Command-line arguments arrive as strings in the ARGS array and typically need parse(Type, ...) before numeric use.
  • The if abspath(PROGRAM_FILE) == @__FILE__ ... end guard 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

Was this page helpful?

Topics covered

#Programming#JuliaStudyNotes#YourFirstJuliaScript#Julia#Script#Writing#Running#StudyNotes#SkillVeris#ExamPrep