LinuxCommandLibrary

alias

Create short names for long commands

TLDR

List all aliases

$ alias
copy

Create a generic alias
$ alias [word]="[command]"
copy

View the command associated to a given alias
$ alias [word]
copy

Remove an aliased command
$ unalias [word]
copy

Turn rm into an interactive command
$ alias [rm]="[rm --interactive]"
copy

Create la as a shortcut for ls --all
$ alias [la]="[ls --all]"
copy

SYNOPSIS

alias [-p] [name[=value] ...]

PARAMETERS

(no arguments)
    Lists all currently defined aliases in the format alias name='value'.

name
    Displays the definition of the alias named name. If name is not an alias, no output is produced.

name=value
    Defines or redefines an alias. name is the alias name, and value is the command string it expands to. The value must be enclosed in single or double quotes if it contains spaces or shell metacharacters.

-p
    Prints all defined aliases in a reusable format (e.g., alias name='value'), suitable for piping or saving to a file.

DESCRIPTION

The alias command is a powerful shell builtin used to create, display, and manage shorthand names for longer or more complex commands. When you type a command, the shell first checks if it is an alias before searching for executables in your PATH. This allows users to personalize their command-line experience, correct common typos, or quickly invoke commands with specific options. For instance, alias ll='ls -alF' defines 'll' as a shortcut for listing directory contents in a long format, including hidden files, and indicating file types. Aliases are highly convenient for frequently used commands, improving productivity and reducing typing. By default, aliases are only active for the current shell session. To make them permanent, they must be defined in shell startup files like ~/.bashrc or ~/.zshrc.

CAVEATS

Aliases are not recursive: an alias's value cannot refer back to itself or to another alias that eventually refers back to it. The shell performs alias expansion only on the first word of a command, unless the alias's value ends with a space. Aliases are shell-specific builtins and are not executable programs in themselves. They only exist for the current shell session unless added to a shell configuration file (e.g., ~/.bashrc).

UNALIAS COMMAND

The unalias command is used to remove alias definitions. Its syntax is unalias [-a] name [name ...]. The -a option removes all alias definitions.

PERSISTENCE

To make aliases permanent across shell sessions, you must add their definitions (e.g., alias ll='ls -alF') to your shell's startup configuration file. For Bash, this is typically ~/.bashrc. For Zsh, it's ~/.zshrc. After modifying these files, you can load the new aliases by sourcing the file (e.g., source ~/.bashrc) or by opening a new terminal session.

ESCAPING ALIASES

If you need to execute the original command that an alias shadows, you can escape the alias. Common methods include:
\command (e.g., \ls)
'command' (e.g., 'ls')
"command" (e.g., "ls")
/bin/command (using the full path, e.g., /bin/ls)

ALIAS EXPANSION RULES

When a command is entered, the shell performs alias expansion as one of its first steps. Key rules include:
1. Only the first word of a command is checked for an alias.
2. If an alias's value ends with a space or tab, the next word on the command line is also checked for alias expansion.
3. Aliases are not recursive; an alias definition cannot refer to itself.

HISTORY

The concept of aliases originated in earlier Unix shells like csh (C shell) and ksh (Korn shell) before being adopted by modern shells such as bash and zsh. They have been a fundamental feature for shell customization and convenience for decades, allowing users to streamline command-line interaction and improve efficiency by creating personalized shortcuts. The core functionality and usage have remained remarkably consistent across different shell implementations.

SEE ALSO

unalias(1), shopt(1), bash(1), zsh(1), functions (shell functions)

Copied to clipboard