JBoss CLI uses many concepts known from standard Unix shells, such as Bash. These include the way variables are handled and how they can be used to store actions (commands and operations). In interactive CLI sessions, however, there is a better way of referring to actions – shell-like aliases managed via alias and unalias commands.

To a certain extent, alias and unalias behave like set and unset for variables, respectively – an alias can be created by passing a key-value pair to the alias command, active aliases can be printed by calling alias with no arguments, and you can unset an alias by passing its name to the unalias command. For example:

[standalone@localhost:9990 /] alias add_system_property='/system-property=foo:add(value=bar)'
[standalone@localhost:9990 /] add_system_property
{"outcome" => "success"}
[standalone@localhost:9990 /] /system-property=foo:read-resource
{
    "outcome" => "success",
    "result" => {"value" => "bar"}
}
[standalone@localhost:9990 /] unalias add_system_property
[standalone@localhost:9990 /] add_system_property
Unexpected command 'add_system_property'. Type 'help --commands' for the list of supported commands.

Of course, an alias might contain variables. Variables are not resolved when we define the alias, but upon its invocation, as one would expect. This can be used to parameterize the command above:

[standalone@localhost:9990 /] alias add_system_property='/system-property=$PROPERTY_NAME:add(value=$PROPERTY_VALUE)'
[standalone@localhost:9990 /] set PROPERTY_NAME=foo PROPERTY_VALUE=bar
[standalone@localhost:9990 /] add_system_property
{"outcome" => "success"}
[standalone@localhost:9990 /] /system-property=foo:read-resource
{
    "outcome" => "success",
    "result" => {"value" => "bar"}
}

It’s easy to see that aliases are very similar to variables. When should you use one over the other then?

The short answer is: Do as you would do in Bash.

The long answer is: Their purpose is different. Aliases are a feature of interactive shells. They cannot be defined in non-interactive scripts. When automating things, you should use variables to refer to operations and commands.

In interactive sessions, variables are a great way to store data (parameters and values), and a useful unit for composing variations of more complicated constructs. Aliases are great for reusable actions (operations and commands).

Aliases are very flexible, can be used for partial commands, and might include whitespace. There are also minor syntactic differences – unlike set, for example, only one alias can be defined in a single invocation of the alias command. The constructs feature standard CLI autocompletion, and calling commands without the $ syntax is just more intuitive and readable. This can be illustrated by the following example:

[standalone@localhost:9990 /] alias add_foo='/system-property=foo:add'
[standalone@localhost:9990 /] add_foo (value=bar)
{"outcome" => "success"}
[standalone@localhost:9990 /] /system-property=foo:read-resource
{
    "outcome" => "success",
    "result" => {"value" => "bar"}
}
[standalone@localhost:9990 /] alias greeting='echo hello'
[standalone@localhost:9990 /] greeting world
hello world

Aliases are bound to a session. However, since they’re only available in interactive shells, they’re typically used for creating shortcuts for common commands used during development. As such, being able to share them across sessions would be very useful. Thankfully, this can be accomplished by specifying aliases in ~/.aesh_aliases. Note that the file is located in user’s home directory, not the server’s home, and it’s processed when the CLI is started.

Æsh (Another Extendable SHell) is a Java library for handling console input. JBoss CLI is built on top of Æsh, and as such, it inherits many of its features, including aliases and their specification in .aesh_aliases. Note that this mechanism works as of WildFly 10.1.0.Final, the most recent version of the runtime, but it might change in a future release.

Aliases shared across sessions can be defined with the same syntax as their session-scoped counterparts. For example:

$ echo "alias print='echo'" >> ~/.aesh_aliases
$ $JBOSS_HOME/bin/jboss-cli.sh -c
[standalone@localhost:9990 /] alias
alias print='echo'
[standalone@localhost:9990 /] print hello world
hello world