How to use command aliases

See also: alias, aliases, unalias, Alias.

[since version 1.8.0]

This document demonstrates how to create, list, execute, and remove aliases.

Contents:

Create an alias

To create an alias that runs a command on a given instance, use the command alias. The code below uses this command to create an alias lscc that will run the command ls inside an instance crazy-cat:

$ multipass alias crazy-cat:ls lscc

After running this command, the alias lscc is defined as running the command ls on the instance crazy-cat. If the alias name lscc is omitted, the alias name defaults to the name of the command to run (ls in this case).

List defined aliases

To see the list of aliases defined so far, use the aliases command:

$ multipass aliases
Alias  Instance   Command
lscc   crazy-cat  ls

Execute an alias

There are two ways to execute the alias.

multipass <alias>

The first way of executing an alias is

$ multipass lscc

This shells into the instance crazy-cat, executes ls and returns to the host command-line, as if it was an exec command.

Arguments are also supported, provided you separate any options with --:

$ multipass lscc -- -l

<alias>

The second way of running an alias is a two-step process:

Add Multipass alias script folder to system path

First, the Multipass alias script folder to the system path. The instructions to do so are displayed the first time one creates an alias, and vary for each platform. For instance,

$ multipass alias crazy-cat:ls lscc
You'll need to add this to your shell configuration (.bashrc, .zshrc or so) for
aliases to work without prefixing with `multipass`:

PATH="$PATH:/home/user/snap/multipass/common/bin"

Linux

Expand to see the instructions for Linux

In Linux, the shell configuration file must be modified. In most Linux distributions, the shell used by default is bash, which can be configured via the file .bashrc in the users home directory. Any text editor can be used for this, for example doing

$ nano ~/.bashrc

Once editing the file, the path can be modified by appending at its end a line such as

export PATH="$PATH:/home/user/snap/multipass/common/bin"

(remember to replace the correct folder by the one given in the output of the Multipass command above and to restart the shell).

In case of using zsh as shell, the file to modify is .zshrc instead of .bashrc; the procedure is the same.

MacOS

Expand to see the instructions for MacOS

In MacOS, the most commonly used shell is zsh. The procedure for adding a folder to the system path is the same as in Linux, described above.

Windows

Expand to see the instructions for Windows

For Windows, however, it is a bit more involved. To make the change permanent, use PowerShell to store the old system path, add the alias folder to it, and store the new path.

$old_path = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).path
$new_path = “$old_path;C:\Users\<user>\AppData\Local\Multipass\bin”
Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $new_path

Don’t forget to restart your terminal. The folder is now permanently added to your path, Multipass can now execute aliases just invoking their name.

Execute the alias

Once you’ve added the alias folder to the system path, you can execute it directly (without mentioning multipass) as below:

$ lscc

This command (given that the path was already added to the system’s path) is equivalent to multipass lscc. Arguments are also supported, without the need for --:

$ lscc -l

Remove an alias

Finally, to remove the alias lscc, issue:

multipass unalias lscc

An alias is also removed when the instance for which it was defined is purged. This means that multipass delete crazy-cat --purge will also remove the alias lscc.


Last updated 2 days ago.