Skip to main content

Understanding Alias Expansion in Linux

In the context of a Linux operating system, alias expansion is a feature provided by the shell to create shortcuts or abbreviations for commands. An alias enables users to define new commands by equating them with a string of one or more existing commands. The main purpose of aliasing is to help users to minimize the keystrokes, remember complex commands, or even correct commonly mistyped commands.

What is Alias Expansion?

Alias expansion occurs when the shell encounters an alias while processing a command line. An alias is essentially a named shortcut for another command. You can create aliases for long or complicated commands, which you can then invoke with a shorter, custom string of characters. When the shell sees this string, it replaces it with the full command defined by the alias. This replacement is what's referred to as "alias expansion."

For example, if you frequently need to list the files in a directory in long format (ls -l), you might create an alias named ll to perform this operation. Now, every time you type ll and hit Enter, the shell replaces ll with ls -l and executes the command.

How Alias Expansion Works in Linux

When a command is entered in the shell, it checks to see if it matches an alias before doing anything else like path resolution or command execution. If the command's initial word matches an alias name, the shell replaces that initial word with the alias's value and then carries out the command. It's worth noting that the shell only looks at the first word of a command for alias expansion.

Creating Aliases

In a typical Bash shell, you can create an alias using the following syntax:

alias alias_name='command_to_run'

For instance, to create an alias for ls -l, you'd run:

alias ll='ls -l'

Listing Aliases

To see a list of all the aliases you have set up in your shell, simply run:


Removing Aliases

If you wish to remove an alias, you can use the unalias command as follows:

unalias alias_name

Permanent Aliases

Aliases created from the command line are session-specific and will be lost once the shell session is closed. To make aliases permanent, you can define them in your shell’s configuration file like .bashrc for Bash or .zshrc for Zsh.

Examples of Alias Expansion

Let's look at some examples to better understand alias expansion.

  1. Basic Alias for ls -l

    alias ll='ls -l'

    After setting this alias, typing ll will now be the same as typing ls -l.

  2. Creating an Alias with Options and Arguments

    alias grep='grep --color=auto'

    Here, grep will always display colorized output, thanks to the --color=auto option.

  3. Alias for Navigating Directories

    alias ..='cd ..'

    This will change to the parent directory when you type .. and hit Enter.

  4. Combining Multiple Commands

    alias update='sudo apt update && sudo apt upgrade'

    The update alias will run both sudo apt update and sudo apt upgrade sequentially.

  5. Alias to Correct Mistyped Commands

    alias cclear='clear'

    If you often mistype clear as cclear, this alias can correct that mistake for you.

  6. Removing an Alias

    unalias ll

    This removes the ll alias, and typing ll will no longer expand to ls -l.

Limitations and Caveats

  • Order of Execution: Aliases take precedence over commands located in your filesystem. This means that if you have an alias with the same name as a system command, the alias will be executed.

  • Non-Interactive Scripts: Aliases are usually not expanded when a shell script is running because shell scripts are non-interactive by default. To enable alias expansion in scripts, you have to set the expand_aliases shell option using shopt -s expand_aliases.

  • Recursion: The shell avoids alias recursion by not expanding any word that is identical to the alias being expanded.


Alias expansion is a powerful feature that can make your life easier while working in a Linux shell. Whether it's for shortening long commands, combining multiple operations into a single command, or even correcting frequent typos, alias expansion is worth understanding and using effectively.

What Can You Do Next 🙏😊

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.

YouTube @cloudaffle