Home > Environment & Utility > alias

alias: Setting Command Aliases

The alias command is used to create, view, and delete aliases, which are shortcuts for longer or frequently used commands. These aliases are only valid for the current shell session and must be added to your shell configuration file for permanent use.

Overview

alias is a powerful feature that helps you use commands more efficiently in the shell. You can use it to simplify complex commands, correct typos, or ensure specific options are always included.

Key Features

  • Command Shorthand: Replace long, frequently used commands with shorter names.
  • Typo Correction: Automatically correct common command typos.
  • Default Option Setting: Ensure specific commands always run with certain options.

Key Options

The alias command itself has a few usage patterns.

Alias Management

Generated command:

Try combining the commands.

Description:

`alias` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Define a new alias

alias ll='ls -alF'

Shorthand the frequently used 'ls -alF' command to 'll'.

View all currently set aliases

alias

Check the list of all aliases defined in the current shell session.

Check the definition of a specific alias

alias ll

Verify which command a specific alias (e.g., 'll') is defined for.

Remove an alias

unalias ll

Delete an alias (e.g., 'll') that is no longer needed. (Use the unalias command)

Override an existing command

alias rm='rm -i'

Set an alias for the rm command to always run in interactive mode (-i) to prevent accidental file deletion.

Tips & Precautions

Tips and points to consider for effective alias usage.

Setting Permanent Aliases

Aliases are only valid for the current shell session and disappear when the shell is closed. For permanent use, add the `alias` command to one of the following files and restart your shell or apply it using the `source` command.

  • Bash: `~/.bashrc` or `~/.bash_profile`
  • Zsh: `~/.zshrc`

Using Quotes

When defining an alias, if the command contains spaces, it must be enclosed in single quotes (') or double quotes ("). Single quotes prevent variable expansion, while double quotes allow it.

  • Example (no variable expansion): `alias myip='ip a | grep inet'`
  • Example (with variable expansion): `alias mydir="ls -l $HOME/Documents"`

Overriding Existing Commands

You can override existing commands with aliases, such as `alias rm='rm -i'`. While this can be useful for safety, it can also lead to unexpected behavior, so exercise caution.

  • To execute a command with an alias as the original command, prepend a backslash (`\`) before the command (`\ls`) or use the `command` keyword (`command ls`).

Same category commands