The Groovy Shell, aka.
groovysh is a command-line application which allows easy access to evaluate Groovy expressions, define classes and run simple experiments.
- Command-line Options and Arguments
- Evaluating Expressions
- User Profile Scripts and State
- Screen Shots
- No need for
gocommand to execute buffer.
- Rich cross-platform edit-line editing, history and completion thanks to JLine.
- ANSI colors (prompt, exception traces, etc).
- Simple, yet robust, command system with online help, user alias support and more.
- User profile support
Command-line Options and Arguments
The shell supports several options to control verbosity, ANSI coloring and other features.
In addition to options, commands or expressions can be given on the command-line which will invoke the shell in non-interactive mode. The commands or expressions will be evaluated and the shell will exit. If no additional arguments are given the shell will startup interactively.
Execute a Command
Evaluate an Expression
When a complete expression is found, it is compiled and evaluated. The result of the evaluation is stored into the _ variable.
Multi-line/complex expressions (like closure or class definitions) may be defined over several lines. When the shell detects that it has a complete expression it will compile and evaluate it.
Define a Class
Use the Class
Shell variables are all untyped (ie. no
def or other type information.
This will set a shell variable:
But, this will evaluate a local variable and will not be saved to the shell's environment:
Functions can be defined in the shell, and will be saved for later use.
Defining a function is easy:
And then using it is as one might expect:
The shell has a number of different commands, which provide rich access to the shell's environment.
Commands all have a name and a shortcut (which is something like
\h). Commands may also have some predefined system aliases. Users may also create their own aliases.
Display the list of commands (and aliases) or the help text for specific command.
The Command List
Help for a Command
While in the interactive shell, you can ask for help for any command to get more details about its syntax or function. Here is an example of what happens when you ask for help for the
Exit the shell.
This is the only way to exit the shell. Well, you can still
CTRL-C, but the shell will complain about an abnormal shutdown of the JVM.
Add a custom import which will be included for all shell evaluations.
This command can be given at any time to add new imports.
Display the contents of the current buffer.
This only displays the buffer of an incomplete expression. Once the expression is complete, the buffer is rest. The prompt will update to show the size of the current buffer as well.
Clears the current buffer, resetting the prompt counter to 000. Can be used to recover from compilation errors.
Show variables, classes or preferences or imports.
Opens the GUI object browser to inspect a variable or the result of the last evaluation.
Purges objects from the shell.
Edit the current buffer in an external editor.
Currently only works on UNIX systems which have the
EDITOR environment variable set, or have configured the
Load one or more files (or urls) into the buffer.
Saves the buffer's contents to a file.
Record the current session to a file.
Display, manage and recall edit-line history.
Create an alias.
Set or list preferences.
Some of aspects of
groovysh behaviors can be customized by setting preferences. Preferences are set using the
set command or the
Set the shell's verbosity level. Expected to be one of:
If this preference is set to an invalid value, then the previous setting will be used, or if there is none, then the preference is removed and the default is used.
Show the last result after an execution.
Sanitize (trim-down/filter) stack traces.
Configures the editor used by the
Default is the value of the system environment variable
Setting a Preference
To list the current set preferences (and their values):
Clearing Preferences (ie. Resetting to Defaults)
User Profile Scripts and State
This script, if it exists, is loaded when the shell starts up.
This script, if it exists, is loaded when the shell enters interactive mode.
Edit-line history is stored in this file.
These shots have been taken over the development of the new shell, so some of the content might look slightly different. Also, note the yellow colors here are the shell's bold color, so the colors might look different depending on how the enclosing shell has its colors setup.
Please report any problems you run into. Please be sure to mark the JIRA issue with the
Problems loading the JLine DLL
On Windows, JLine (which is used for the fancy shell input/history/completion fluff), uses a tiny DLL file to trick the evil Windows faux-shell (
COMMAND.COM) into providing Java with unbuffered input. In some rare cases, this might fail to load or initialize.
One solution is to disable the frills and use the unsupported terminal instance. You can do that on the command-line using the
--terminal flag and set it to one of:
Problems with Cygwin on Windows
Some people have issues when running groovysh with cygwin. If you have troubles, the following may help: