Linux online course

Create and Manage Command Aliases in Linux

Learn how Linux shell aliases work, how to create, inspect, remove, bypass, and persist shortcuts in Bash and Zsh.

What Is a Linux Shell Alias?

An alias is a shortcut defined by a shell, such as Bash or Zsh. It replaces one command name with another command string before the shell runs the command. This replacement is called command expansion.

For example, an alias can replace the short name dir with ls -l. When you type dir, the interactive shell expands it and runs ls -l.

An alias can represent:

  • A longer command, such as dir for ls -l.
  • A command with preferred options, such as ls for ls -l.
  • A short sequence of commands, such as a directory listing followed by pwd.

An alias is not a standalone Linux executable. It exists inside the shell that defines it and normally affects commands entered interactively at that shell prompt.

Why Create Aliases?

  • Use memorable names: Give an unfamiliar or lengthy command a name that is easier to remember.
  • Reduce typing: Create short names for commands you use frequently.
  • Set preferred defaults: Make commonly used options automatic for interactive commands.
  • Provide compatibility shortcuts: Users familiar with commands from another operating system can create familiar names such as dir.
  • Bundle a small repeated sequence: Run two simple commands from one shortcut.

Alias Syntax

The general form is:

alias NAME='command and options'

NAME is the shortcut you type. The text inside the quotes is the replacement command, including any options.

Quoting is important because the replacement usually contains spaces, options, shell metacharacters, or multiple commands. Single quotes are the normal convention because they preserve the replacement text while the alias is defined.

alias dir='ls -l'

In this example, dir is the alias name and ls -l is its replacement.

Creating a Simple Alias

Create a directory-listing shortcut in the current shell:

alias dir='ls -l'

Now invoke it like any other command:

dir

The shell expands dir to ls -l, so the command displays a long-format directory listing.

This definition is temporary. It remains available in the current shell session, but normally disappears when that shell closes. To retain it in future sessions, add it to the appropriate shell configuration file.

Aliases with Command Options

An alias can change the routine interactive behavior of an existing command. For example:

alias ls='ls -l'

After this definition, typing ls in the current interactive shell expands to ls -l. The alias overrides the normal interactive interpretation of the command name.

Sometimes you need the original command without the alias. Prefix the command name with a backslash:

\ls

The leading backslash prevents alias expansion for that invocation. You can also inspect or remove the alias if you no longer want the changed behavior.

Aliases That Run Multiple Commands

Command separators allow an alias to run more than one command:

alias dirinfo='ls -l; pwd'

Running dirinfo first displays the directory contents and then prints the current working directory.

A semicolon runs the next command whether or not the previous command succeeds. Conditional chaining with && runs the second command only when the first succeeds:

alias dirinfo='ls -l && pwd'

Use ; when both commands should be attempted. Use && when the later command depends on successful completion of the earlier one.

Aliases are suitable for small, predictable sequences. More complicated control flow, error handling, or parameter handling is usually better implemented as a shell function or script.

Listing and Inspecting Aliases

Run alias with no arguments to display aliases currently defined in the shell:

alias

To inspect one named alias, provide its name:

alias ls

If the alias exists, the shell prints its definition. If it does not exist, the shell reports an error or produces no matching definition, depending on the shell.

Some displayed aliases may have been supplied by the shell, a distribution configuration, or another startup file. Therefore, the complete list may contain definitions you did not create at the prompt.

Removing Aliases

Use unalias to remove one alias from the current shell:

unalias dir

unalias changes only the active shell session. If the alias was also written in a startup file, it will return when that file is loaded again. Remove or edit the persistent definition there as well.

Some shells support removing all aliases with an option such as unalias -a. Treat this as an advanced operation: it can discard useful defaults and aliases supplied by your shell configuration.

Alias Management Commands

TaskCommand patternOutcome
Create an aliasalias NAME='command options'Defines an alias in the current shell.
List all aliasesaliasDisplays aliases currently known to the shell.
Display one aliasalias NAMEPrints the definition of the named alias.
Remove one aliasunalias NAMERemoves the alias from the current shell.
Bypass an alias once\commandRuns the command name without alias expansion for that invocation.

Temporary and Persistent Aliases

An alias entered at the prompt is temporary because it is stored only in the current shell process. A persistent alias is written to a shell startup file, a configuration file read when a shell starts.

Bash

For interactive Bash sessions, ~/.bashrc is the common configuration file. Add an entry such as:

alias dir='ls -l'

Reload the file in the current Bash session with:

source ~/.bashrc

The source builtin reads and executes the file in the current shell, so the alias becomes available without opening another terminal.

Zsh

For interactive Zsh sessions, the common file is ~/.zshrc. Add the same alias definition:

alias dir='ls -l'

Reload it with:

source ~/.zshrc

You can also close and reopen the terminal to start a new interactive shell.

The correct file depends on the active shell and on whether the shell is a login shell, an interactive shell, or both. A file used by Bash is not automatically used by Zsh. Startup behavior can also be customized by system or user configuration. See Bourne Again Shell (Bash) for Bash-specific background.

Common Shell Configuration Locations

ShellTypical interactive configuration fileHow to reloadNotes
Bash~/.bashrcsource ~/.bashrcCommonly used for interactive non-login Bash sessions; login configuration may load it indirectly.
Zsh~/.zshrcsource ~/.zshrcCommonly used for interactive Zsh sessions.
Other shellsShell-specific fileUse that shell's source or reload mechanismCheck the shell's documentation and login versus interactive startup rules.

Aliases, Functions, and Scripts

Aliases are simple text substitutions. In general, they do not accept positional arguments in the flexible way that functions do. For example, an alias is not a reliable replacement for a command that needs to process each argument separately.

Aliases may also fail to expand in non-interactive scripts. Some shells provide options that change this behavior, but scripts should not depend on interactive aliases. Use explicit commands, variables, functions, or separate scripts for automation.

CapabilityAliasShell functionScript
Simple command shortcutExcellentSuitableSuitable but often unnecessary
Default command optionsExcellentSuitableSuitable for a separate command workflow
Multiple commandsSuitable for small sequencesBetter for control flowBest for larger automation
Accepting argumentsLimited and unreliable for robust handlingYes, with positional parametersYes, with script arguments
Use in automationGenerally avoidUseful when explicitly loadedBest choice for reusable automation
Persistence methodShell startup fileShell startup file or a file that is sourcedSaved executable file in a suitable path

Use an alias for a short interactive convenience. Use a function when the shortcut needs parameters, conditions, loops, or safer argument handling. Use a script when the operation should be a reusable automation tool.

Troubleshooting Aliases

The Alias Disappears After Closing the Terminal

The definition was probably entered only at the prompt. Add it to the startup file for the active shell, then run source on that file or open a new terminal.

An Alias in a Configuration File Is Not Recognized

Common causes include editing the wrong startup file, using the wrong shell, not reloading the file, or having a quoting or syntax error. Check which shell is active, place the definition in its appropriate interactive configuration file, validate the definition, and reload it.

Spaces or Options Are Not Preserved

The replacement was probably not quoted correctly. Use the standard form:

alias NAME='command options'

If the replacement itself needs quoted text, carefully nest or escape the quotes so the outer alias definition remains valid.

ls Uses Unexpected Options

Inspect the definition:

alias ls

Bypass it once with:

\ls

Or remove it for the current session:

unalias ls

If it returns later, edit the corresponding startup file and reload it.

The Alias Does Not Work in a Script

Aliases are primarily interactive-shell conveniences and may not expand in non-interactive scripts. Replace the alias with the full command, a function, or a separate script.

The Shortcut Needs User-Provided Arguments

That is a sign that an alias is the wrong tool. Replace it with a shell function that accepts positional parameters, or write a script if the operation is substantial.

Exam-Relevant Notes

  • An alias is defined by the shell, not by the Linux kernel as an executable file.
  • alias NAME='replacement' creates a definition in the current shell session.
  • alias lists aliases, while alias NAME inspects one alias.
  • unalias NAME removes one alias from the current shell.
  • \command bypasses alias expansion for one command invocation.
  • ~/.bashrc is a common Bash interactive startup file, and ~/.zshrc is a common Zsh interactive startup file.
  • source FILE reads a configuration file into the current shell.
  • Functions are generally better than aliases for arguments and complex logic; scripts are better for reusable automation.