The Groovy Shell, aka. groovysh is a command-line application which allows easy access to evaluate Groovy expressions, define classes and run simple experiments.
go command to execute buffer.The shell supports several options to control verbosity, ANSI coloring and other features.
./bin/groovysh --help usage: groovysh [options] [...] -C, --color[=FLAG] Enable or disable use of ANSI colors -D, --define=NAME=VALUE Define a system property -T, --terminal=TYPE Specify the terminal TYPE to use -V, --version Display the version -d, --debug Enable debug output -h, --help Display this help message -q, --quiet Suppress superfluous output -v, --verbose Enable verbose output |
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.
./bin/groovysh 'show preferences' No preferences are set |
./bin/groovysh 'System.properties.each { k, v -> println("$k = $v") }'
java.runtime.name = Java(TM) 2 Runtime Environment, Standard Edition
sun.boot.library.path = /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Libraries
java.vm.version = 1.5.0_07-87
awt.nativeDoubleBuffering = true
gopherProxySet = false
...
|
println "Hello" |
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.
class Foo {
def bar() {
println "baz"
}
}
|
foo = new Foo() foo.bar() |
Shell variables are all untyped (ie. no def or other type information.
This will set a shell variable:
foo = "bar" |
But, this will evaluate a local variable and will not be saved to the shell's environment:
def foo = "bar" |
Functions can be defined in the shell, and will be saved for later use.
Defining a function is easy:
groovy:000> def hello(name) {
groovy:001> println("Hello $name")
groovy:002> }
|
And then using it is as one might expect:
hello("Jason")
|
Internally the shell creates a closure to encapsulate the function and then binds the closure to a variable. So variables and functions share the same namespace. |
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.
helpDisplay the list of commands (and aliases) or the help text for specific command.
groovy:000> help
For information about Groovy, visit:
http://groovy.codehaus.org
Available commands:
help (\h ) Display this help message
? (\? ) Alias to: help
exit (\x ) Exit the shell
quit (\q ) Alias to: exit
import (\i ) Import a class into the namespace
display (\d ) Display the current buffer
clear (\c ) Clear the buffer
show (\S ) Show variables, classes or imports
inspect (\n ) Inspect a variable or the last result with the GUI object browser
purge (\p ) Purge variables, classes, imports or buffers
edit (\e ) Edit the current buffer
load (\l ) Load a file or URL into the buffer
. (\. ) Alias to: load
save (\s ) Save the current buffer to a file
record (\r ) Record the current session to a file
history (\H ) Display, manage and recall edit-line history
alias (\a ) Create an alias
set (\= ) Set (or list) preferences
For help on a specific command type:
help <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 help command:
groovy:000> help help usage: help [<command>] Display the list of commands or the help text for <command>. |
exitExit 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.
importAdd a custom import which will be included for all shell evaluations.
This command can be given at any time to add new imports.
displayDisplay 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.
groovy:000> class Foo {
groovy:001> def bar
groovy:002> def baz() {
groovy:003> display
001> class Foo {
002> def bar
003> def baz() {
|
clearClears the current buffer, resetting the prompt counter to 000. Can be used to recover from compilation errors.
showShow variables, classes or preferences or imports.
show variablesgroovy:000> show variables Variables: _ = true |
show classesshow importsshow preferencesshow allinspectOpens the GUI object browser to inspect a variable or the result of the last evaluation.
purgePurges objects from the shell.
purge variablespurge classespurge importspurge preferencespurge alleditEdit the current buffer in an external editor.
Currently only works on UNIX systems which have the EDITOR environment variable set, or have configured the editor preference.
loadLoad one or more files (or urls) into the buffer.
saveSaves the buffer's contents to a file.
recordRecord the current session to a file.
record startrecord stoprecord statushistoryDisplay, manage and recall edit-line history.
history showhistory recallhistory flushhistory clearaliasCreate an alias.
setSet or list preferences.
Some of aspects of groovysh behaviors can be customized by setting preferences. Preferences are set using the set command or the \= shortcut.
verbositySet the shell's verbosity level. Expected to be one of:
DEBUGVERBOSEINFOQUIETDefault is INFO.
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-last-resultShow the last result after an execution.
Default is true.
sanitize-stack-traceSanitize (trim-down/filter) stack traces.
Default is true.
editorConfigures the editor used by the edit command.
Default is the value of the system environment variable EDITOR.
To use
|
set verbosity DEBUG |
To list the current set preferences (and their values):
show preferences |
At the moment, there is no way to list all of the known/available preferences to be set. |
purge preferences |
$HOME/.groovy/groovysh.profileThis script, if it exists, is loaded when the shell starts up.
$HOME/.groovy/groovysh.rcThis script, if it exists, is loaded when the shell enters interactive mode.
$HOME/.groovy/groovysh.historyEdit-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 Groovysh component.
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 (CMD.EXE or 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:
nonefalseoffjline.UnsupportedTerminalgroovysh --terminal=none |
Some people have issues when running groovysh with cygwin. If you have troubles, the following may help:
stty -icanon min 1 -echo groovysh --terminal=unix stty icanon echo |
wiki