Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Julia Interactive Commands

The Julia Interactive Command Window

Running the julia command enters the interactive command window directly:

$ julia 

               _

   _       _ _(_)_     |  Documentation: https://docs.julialang.org

  (_)     | (_) (_)    |

   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.

  | | | | | | |/ _` |  |

  | | |_| | | | (_| |  |  Version 1.7.2 (2022-02-06)

 _/ |\__'_|_|_|\__'_|  |  release-1.7/bf53498635 (fork: 461 commits, 259 days)

|__/                   |



julia> 

Run exit() to leave the interactive command window; you can also exit by typing CTRL-D (pressing the Ctrl key and the d key together).

Type a complete expression and press Enter, and the REPL computes and displays its value:

Example

julia

1

2

3

julia

ans

3

ans is a special variable that holds the result of the last expression; it is valid only in interactive sessions.

Run exit() to leave the interactive command window; you can also exit by typing CTRL-D (pressing the Ctrl key and the d key together).


The Four Command Modes of the REPL

The Julia REPL has four built-in working modes, switched with different keys:

Diagram of Switching Between the Four Julia REPL Command Modes

Mode Key to enter Prompt Purpose
Julia mode (default) julia> Execute Julia code
Help mode ? help?> Query the documentation of a function or type
shell mode ; shell> Execute system shell commands
Package management mode ] pkg> Manage software packages

In any mode, press Backspace or Ctrl-C to return to the default Julia mode.

Help mode

Type ? at an empty prompt, then enter the name of the function you want to look up to view its documentation, for example looking up println:

help?> println

search: println print printstyled sprint isprint



  println([io::IO], xs...)

  Print (using print) xs followed by a newline.

When you encounter an unfamiliar function, using help mode to check the docs is the fastest way to learn on your own.

shell mode

Type ; to enter shell mode, where you can directly run system commands like ls and cd:

shell> ls

hello.jl  runoob_test.jl

Package management mode

Type ] to enter Pkg mode, where you can install and manage packages:

pkg> add Example        # 安装 Example 包

pkg> rm Example         # 卸载包

pkg> status             # 查看已安装的包

The detailed usage of package management will beModules and Package Managementcovered in the chapter.


Running Julia Code Files

Besides interactive use, we can also execute a Julia code file whose name ends in .jl.

Here is a file named runoob_test.jl:

The runoob_test.jl file

Outputs three lines of content in sequence

println

(

"Hello World!"

)

println

(

"RUNOOB"

)

println

(

1

1

)

To execute Julia language code, use the julia runoob_test.jl command.

Executing the above code outputs:



$ julia runoob_test.jl

Hello World!

RUNOOB

2

Loading Files in the REPL

In the REPL you can use the include() function to load and execute a source file:

Example

julia

include

(

"runoob_test.jl"

)

Hello World

!

RUNOOB

2

Note:include() executes all the code in the file in the current session, suitable for reloading during development and debugging; while the julia command starts a new process to run the file, suitable for running a complete program.


Common REPL Shortcuts

Operation Description
Ctrl-D Exit the REPL
Ctrl-C Interrupt the currently executing computation
Ctrl-L Clear screen
Tab Auto-complete function names and variable names (also used to enter LaTeX symbols)
Up/Down arrow keys Browse input history
Ctrl-R Reverse-search input history
; End Adding a semicolon at the end of an expression suppresses output of the result

Tab completion also supports Unicode symbols: typing \pi and pressing Tab automatically turns it into pi, which is one of Julia's distinctive features.

Example

Type \pi then press Tab to get pi

julia

π

π =

3.1415926535897

...

Type \sqrt then press Tab to get the square root sign, which can be used directly as a square root

julia

√

2

1.4142135623730951