A brief introduction to Julia’s REPL, packages and basic I/O
This is part 3 of the Julia Tutorials (a series of tutorials on Julia). If Julia is not installed in your system, check out part 2 of this series.
If you are not familiar with Julia and want to get started with it, feel free to check out the complete series @ —
REPL in Julia
Julia comes with a full featured REPL (read-eval-print loop) interactive command line built into julia’s executable. Not only does it allow Julia statements to be evaluated quickly and easily, it also has a searchable history, tab completion, many helpful keyboard shortcuts, and special help and shell modes. The REPL can be started by simply calling julia
from your command line
To exit the session, just type exit()
and hit enter or use keyboard shortcut ctrl + D
.
The REPL has 4 main modes of operation:
- Julian prompt — This is the default mode in which all newlines start with
julia>
. You can run any Julia code blocks in this mode.
- Help mode — The Julia prompt can be switched to help mode by simply typing
?
in a newline. You can get documentation for anything entered in this mode.
- Shell mode — Sometimes, we may want to use system shell to execute system commands, Julia offers a way to execute shell commands within the REPL . To access shell mode just type
;
in a newline
- Search mode — Julia REPL has a searchable history in which each lines will be saved to a history file. To search through this history, just use the keyboard shortcut
Ctrl + R
Packages in Julia
Julia offers a built-in package manager known as Pkg
which handles operations like installing, updating, removing packages. Pkg
can be accessed from REPL by typing ]
in a newline
To exit the package manager, use the keyboard shortcut Ctrl + C
Installing Packages
Use the add
keyword to install required packages.
(v1.0) pkg> add JSON StaticArrays
Installs packages JSON
and StaticArrays
Updating Packages
Use the update
keyword to update desired packages
(v1.0) pkg> update JSON
If you want to update all packages, then simply use the update
keyword without any package names.
(v1.0) pkg> update
Removing Packages
Use rm
keyword to remove packages
(v1.0) pkg> rm JSON
I/o methods for stdin and stdout
In Julia, data from standard input stream(STDIN) can be read using readline
or readlines
- readline — This method reads a line of text as
String
from the input stream (STDIN) until a ‘\n’ (newline) character is encountered. - readlines — This method is used to read N lines of texts entered from the console as entries of a one-dimensional
String
array. Here lines must be delimited by a line break character (“\n”) or by pressing the “ENTER” key. To finish typing, press Ctrl-D.
In Julia, data can be send to standard output stream(STDOUT) using either print
or println
- print — Writes the given data to the output stream
- println — Writes the given data to the output stream and inserts a newline character (“\n”) at the end.
Next Up — Variables and Types in Julia