How to debug script in Julia

In Julia's ecosystem, the Debugger.jl package allows us to navigate the program code and inspect its execution flow.

This process, known as code stepping, is active only in the full-fledged and interactive command-line REPLRead-Eval-Print Loop built within Julia's executable. Running Julia's statements from the command-line is typically quick and more intuitive.

The debugger package helps find errors and can substantially reduce the time it takes to perfect our program. It provides lots of functions as follows:

Category

Command

Description

Stepping in and out

n

It helps move to the next line


s

It helps move to the next call


sr

It helps move until next return


st

It shows the status of the current function

Evaluation

w add expr

It inserts an expression into the evaluation list


w

It displays all watched and evaluated expressions

Breakpoints

bp

It displays all configured breakpoints


bp add "file.jl":line [cond]

It configures a breakpoint in the file file.jl in the line with the condition cond


bp add func [:line] [cond]

It inserts a breakpoint to function func in the line (first line by default) with condition cond

Note: Please visit the official documentation https://github.com/JuliaDebug/Debugger.jl/blob/master/README.mdfor a complete list of commands.

Let's discover how to debug Julia's script step by step.

Example

The example below illustrates the steps required to scrutinize a Julia script in a gradual manner:

echo 'Start Julia in command-line REPL mode'
echo ' using Debugger
       println(PROGRAM_FILE);
       f = function(a = 1, b = 2)
            print("Start")
            c = 3
            print(c)
            output = a + b + c
            print(output)
            print("End")
       end
     ' > script.jl
Debugging a script

Let's explain the code widget above:

  • Line 1: Display a message inviting the user to run Julia in command-line REPL mode.

  • Lines 2–12: Devise a script that will:

    • Line 2: Load the Debugger package.

    • Line 3: Print the name of the script involved.

    • Lines 5–12: Define a simple function to debug and generate its code dynamically to a file called script.jl.

When running this code widget, we should follow the steps listed below:

Step 1: Go to the "Terminal" tab and run the following command to execute the script elaborated above: bash /usercode/script.sh

Generate the script
Generate the script

Step 2: Type Julia and run it in command-line REPL mode.

Launch Julia
Launch Julia

Step 3: Run the script named script.jl as given below.

Run the script
Run the script

Step 4: Initiate the debugger by invoking the @enter command along with the function f as defined in the code above.

Initiate the debugger
Initiate the debugger

Step 5: Type n multiple times to move progressively from one line to another until we reach the end of the script. Every time, we press n the line to be executed is exhibited in the About to run: as shown in the following image.

Debug the script
Debug the script

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved