0Pricing
Groovy & Gradle: JVM Automation and Build Engineering · Lesson

Groovy Scripts & GShell

Learn to write and execute Groovy scripts, and interact with the Groovy console (GroovyShell).

Groovy Scripts & GShell is a free Groovy & Gradle: JVM Automation and Build Engineering lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Groovy & Gradle: JVM Automation and Build Engineering learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Groovy Scripts: Quick Automation

A Groovy script is just a file of code that runs directly — no main method required. Perfect for automation and quick utilities.

Simple Script Structure

A Groovy script needs no class or method wrapper — the file itself is the entry point. Just write your lines and run them.

println "Hello from a Groovy script!"
def name = "Coddy"
println "My name is $name"

Executing Groovy Scripts

Save your code with a .groovy extension, then run it from the terminal with groovy myScript.groovy. That is the whole workflow.

Script Example: Basic Calculation

This script adds two numbers and branches with an if — a quick taste of how natural and readable Groovy scripting feels.

def num1 = 10
def num2 = 5
def sum = num1 + num2
println "The sum of $num1 and $num2 is: $sum"

if (sum > 10) {
  println "That's a big sum!"
}

Getting User Input

Scripts can talk to the user too. Read a line from the console with System.in.newReader().readLine(), as shown below.

print "What's your name? "
def name = System.in.newReader().readLine()
println "Hello, $name! Welcome to Groovy."

Meet the GroovyShell (GShel)

The GroovyShell (groovysh) is an interactive console — run snippets instantly, test ideas, and get feedback without saving a file.

Launching GShell

Type groovysh to launch the interactive shell, or groovyConsole for the GUI version. We will stick with the command-line one here.

Your First GShell Commands

Inside groovysh, type any Groovy and it runs immediately, showing the result after ===>. Type :exit when you are done.

GShell: Beyond Code

GShell has colon commands for managing your session: :help, :history, :clear, and :reset to wipe declared variables.

Instant Code Testing

GShell shines for instant experiments — call a method or transform a list and see the result right away, no file, compile, or run step.

Script vs. GShell

Consider the following Groovy code snippet:

def x = 5
println x * 2

Where would you typically execute this snippet if you want to save it for later use and run it repeatedly?

Recap: Scripts & GShell

You learned the two core Groovy tools: scripts for repeatable .groovy files, and the GroovyShell for instant experimentation.

Frequently asked questions

Is the “Groovy Scripts & GShell” lesson free?

Yes — the full text of “Groovy Scripts & GShell” is free to read here on the web, and the Groovy & Gradle: JVM Automation and Build Engineering course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Groovy & Gradle: JVM Automation and Build Engineering course, upgrade to CoddyKit PRO.

What will I learn in “Groovy Scripts & GShell”?

Learn to write and execute Groovy scripts, and interact with the Groovy console (GroovyShell). You practise Groovy & Gradle: JVM Automation and Build Engineering with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Groovy & Gradle: JVM Automation and Build Engineering?

No prior experience is required. Groovy & Gradle: JVM Automation and Build Engineering on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Groovy Scripts & GShell” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Groovy & Gradle: JVM Automation and Build Engineering lesson?

Yes. Every Groovy & Gradle: JVM Automation and Build Engineering lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Introduction to Groovy & JVM
  2. Groovy Syntax & Basic Types
  3. Groovy Scripts & GShell
  4. Groovy Strings & GStrings
← Back to Groovy & Gradle: JVM Automation and Build Engineering