alias

shelllinux
The alias command is one of the most frequently used commands in Linux/Unix-like operating systems. alias The alias command creates or lists command aliases, allowing users to create shortcuts for longer commands or to customize command behavior with predefined options.

Quick Reference

Command Name:

alias

Category:

shell

Platform:

linux

Basic Usage:

alias [options] [arguments]

Common Use Cases

    Syntax

    alias [name[=value] ...]

    Options

    The alias command doesn't have traditional command-line options, but rather uses the following format:

    Format Description
    alias Without arguments, it lists all currently defined aliases
    alias name Lists the alias definition for the specified name
    alias name=value Defines an alias called 'name' with the value 'value'

    Examples

    How to Use These Examples

    The examples below show common ways to use the alias command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    #

    Basic Examples:

    # Create a simple alias
    alias ll='ls -la'
    # List all defined aliases alias
    # Create an alias with multiple commands alias update='sudo apt update && sudo apt upgrade -y'
    # Create an alias with quotes alias echohi='echo "Hello World"'

    Advanced Examples:

    # Create an alias with complex command substitution
    alias gitlog='git log --graph --pretty=format:"%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset" --abbrev-commit'
    # Create an alias that uses parameters alias mcd='function _mcd() { mkdir -p "$1"; cd "$1"; }; _mcd'
    # Create an alias for safer file operations alias rm='rm -i' alias cp='cp -i' alias mv='mv -i' # Create an alias that includes environment variables alias path='echo $PATH | tr ":" "\n"' # Create an alias with sudo that preserves the environment alias sudoe='sudo -E'

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    Understanding Alias Behavior:

    The alias command is a shell builtin in Bash and most other shells. Key points to understand:

    • Aliases defined on the command line only exist for the current shell session
    • To make aliases permanent, add them to your shell's initialization file (e.g., ~/.bashrc, ~/.zshrc)
    • Aliases are expanded when a command is read, not when it's executed
    • Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set
    • The first word of each simple command is checked for an alias

    Alias Limitations:

    Aliases have some limitations compared to functions:

    • Aliases cannot contain parameters or arguments (unlike functions)
    • Complex command sequences are better defined as shell functions
    • Aliases cannot be exported to subshells or child processes
    • Alias names cannot contain special characters except underscores
    • The alias value must be quoted if it contains spaces or special characters

    Making Aliases Permanent:

    To make aliases permanent, add them to your shell configuration file:

    # For Bash users
    echo "alias ll='ls -la'" >> ~/.bashrc
    source ~/.bashrc
    
    # For Zsh users
    echo "alias ll='ls -la'" >> ~/.zshrc
    source ~/.zshrc
    
    # For Fish shell users
    echo "alias ll='ls -la'" >> ~/.config/fish/config.fish
    source ~/.config/fish/config.fish
    

    Disabling Aliases:

    There are several ways to bypass or remove aliases:

    • Use \command to bypass an alias once (backslash before the command)
    • Use command command_name to bypass an alias
    • Use unalias name to remove a specific alias
    • Use unalias -a to remove all aliases

    Common Use Cases:

    • Creating shortcuts for frequently used commands
    • Adding default options to commands (e.g., alias grep='grep --color=auto')
    • Creating safer versions of commands (e.g., alias rm='rm -i')
    • Correcting common typos (e.g., alias sl='ls')
    • Combining multiple commands into a single command

    Important Notes:

    • Recursive aliases are prevented by the shell (an alias cannot call itself directly)
    • When setting up an alias that uses the same command name, be careful to include all necessary options
    • To include variables in aliases that will be expanded at runtime, use single quotes for the alias definition
    • For more complex requirements, consider using shell functions instead of aliases
    • The type command can be used to see if a command is an alias, builtin, or external program

    Related Commands

    These commands are frequently used alongside alias or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the alias command works in different scenarios.

    $ alias
    View All Commands