Julia Interactive Commands
Julia REPL (Read-Eval-Print Loop) is Julia's interactive command-line environment, an excellent tool for learning and experimenting with Julia code.
Starting REPL
Enter julia in terminal to start REPL:
_
_ _ _(_)_ | Documentation: https://docs.julialang.org
(_) | (_) (_) |
_ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 1.10.0 (2023-12-25)
_/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |
julia>
Basic Usage
Executing Expressions
Enter expressions directly and press Enter:
julia> 1 + 2
3
julia> "Hello, " * "Julia!"
"Hello, Julia!"
julia> sqrt(16)
4.0
Variable Assignment
julia> x = 10
10
julia> y = 20
20
julia> x + y
30
Use semicolon ; to suppress output:
julia> a = 1;
julia> b = 2;
julia> a + b
3
For multi-line code, enter directly:
julia> function greet(name)
println("Hello, $name!")
end
greet (generic function with 1 method)
julia> greet("Julia")
Hello, Julia!
REPL Modes
Julia REPL has four main modes, switched using special keys.
1. Julian Mode (Default)
Prompt: julia>
Used for executing Julia code.
2. Help Mode
Press ? to enter, prompt changes to: help?>
help?> println
search: println printstyled print sprint isprint
println([io::IO], xs...)
Print (using print) xs to io followed by a newline.
If io is not supplied, prints to the default output stream stdout.
Press Backspace to return to Julian mode.
3. Package Mode
Press ] to enter, prompt changes to: (@v1.10) pkg>
(@v1.10) pkg> status
Status `~/.julia/environments/v1.10/Project.toml`
[336ed68f] CSV v0.10.12
[a93c6f00] DataFrames v1.6.1
Common commands:
pkg> add PackageName # Install package
pkg> rm PackageName # Remove package
pkg> update # Update all packages
pkg> status # View installed packages
pkg> instantiate # Install dependencies from Project.toml
Press Backspace to return to Julian mode.
4. Shell Mode
Press ; to enter, prompt changes to: shell>
shell> pwd
/home/user
shell> ls
Documents Downloads julia-project
Press Backspace to return to Julian mode.
Keyboard Shortcuts
Editing Shortcuts
Line Editing Shortcuts
Auto-Completion
Press Tab for auto-completion:
Function Name Completion
julia> prin<Tab>
print println printstyled
Module Member Completion
julia> using LinearAlgebra
julia> LinearAlgebra.<Tab>
BLAS Diagonal Factorization ...
Unicode Completion
Julia supports Unicode characters via LaTeX-style input:
julia> \alpha<Tab>
α
julia> \sqrt<Tab>
√
julia> \pi<Tab>
π
Common Unicode symbols:
History
View History
Use up/down arrow keys to browse previous commands.
Search History
Press Ctrl + R to enter reverse search mode:
(reverse-i-search)`pri': println("Hello")
History File
History is saved in ~/.julia/logs/repl_history.jl.
Special Variables
ans
ans stores the result of the last expression:
julia> 1 + 2
3
julia> ans * 2
6
Running Scripts
Run Script in REPL
julia> include("myscript.jl")
Command Line Script
With Arguments
julia myscript.jl arg1 arg2
Access arguments in script:
# myscript.jl
for arg in ARGS
println(arg)
end
Startup Options
Common Command Line Options
# Specify thread count
julia -t 4
# Execute expression
julia -e 'println("Hello")'
# Quiet mode (no banner)
julia -q
# Enter REPL after running script
julia -i script.jl
# Optimization level
julia -O3 script.jl
# View help
julia --help
Startup File
Julia automatically executes ~/.julia/config/startup.jl on startup:
# ~/.julia/config/startup.jl
println("Welcome to Julia!")
# Customize REPL behavior
atreplinit() do repl
# Custom REPL initialization
end
Practical Tips
1. Quick Function Testing
julia> f(x) = x^2 + 2x + 1
f (generic function with 1 method)
julia> f(3)
16
julia> f.(1:5) # Vectorized call
5-element Vector{Int64}:
4
9
16
25
36
2. View Types
julia> typeof(1)
Int64
julia> typeof(1.0)
Float64
julia> typeof("hello")
String
3. View Methods
julia> methods(+)
# [Shows all methods of + operator]
4. Timing
julia> @time sum(1:1000000)
0.000001 seconds
500000500000
5. Memory Allocation
julia> @allocated rand(1000)
8000
Exiting REPL
Multiple ways to exit:
julia> exit()
# Or press Ctrl + D (on empty line)
Next Steps
After mastering REPL usage, continue learning: