In Using environment variables in JBoss CLI, we described how to pass environment variables to CLI scripts. This brings us to a related concept – not unlike Unix shells, CLI also allows us to define variables for use inside scripts and interactive sessions.

The basic commands for defining and undefining variables are set and unset, respectively. The $ symbol can be used to reference a variable, and its value can be printed using the echo command. In general, tho whole system is very similar to what we’re used to from standard Unix shells, so an example should be enough to explain the basic concepts:

Multiple variables can be managed in a single command, such as:

Using set without arguments prints all the variable assignments:

The system is relatively versatile – variables can refer to various things, including operations, commands, parameters, values etc. For example:

Note that the scope of variables is, similarly to Bash and other Unix shells, tied to a session. If you quit the session which you defined the variables in and relaunch the CLI, variable assignments will be lost. Similarly, if a variable is set in a script, it will be undefined in a different script executed via a separate invocation of the CLI.