Where to start First steps track · 15 stages
Linux Labs

alias — command shortcuts

4 min bash

Short names for long commands; temporary and permanent via ~/.bashrc; why alias is invisible in scripts.

Cheat sheet

5 commands Printable version
  • alias ll='ls -lah' create
  • alias list
  • unalias ll remove
  • \cmd / command cmd run the original, bypassing the alias
  • echo "alias ..='cd ..'" >> ~/.bashrc make it permanent

Overview

alias is a shortcut: a short name the shell expands into a command. It is handy for frequent commands with flags. It is an interactive-shell feature — aliases do not work in scripts by default (write functions there instead, see bash-scripting).

Options

2

alias barely has any flags: it takes name='command' pairs. The useful ones are -p (print the list in a form suitable for re-execution, same as with no arguments) and unalias -a (remove every alias of the current session at once).

An alias created on the command line lives only until you close the terminal. To make it permanent, add it to ~/.bashrc (per user):

bash
echo "alias ll='ls -lah'" >> ~/.bashrc
source ~/.bashrc              # apply in the current session (or open a new terminal)

Ubuntu's default system aliases live in ~/.bash_aliases (sourced by ~/.bashrc). More on profiles and $PATH — env-vars.

An alias does not take arguments the way a beginner expects: alias gl='git log' gets substituted before the rest of the line, so gl -5 works (it becomes git log -5), but an alias cannot insert an argument in the middle of a command — that needs a shell function (see bash-scripting).

Option What it does Example
-p Print all current aliases in a form suitable for re-execution (same as with no arguments). alias -p
-a Remove all aliases of the current session at once. dangerous unalias -a

Nothing found. Try a shorter query.

Recipes

1

Examples

bash
alias                         # show all current aliases
alias ll='ls -lah'            # create an alias
alias gs='git status'
alias ..='cd ..'
alias grep='grep --color=auto'
type ll                       # check what it expands to (see which/type)
unalias ll                    # remove one
\ls                           # run the ORIGINAL, bypassing the alias (backslash)
command ls                    # same — without alias expansion

Troubleshooting

3

The alias is gone after reopening the terminal

You created it on the command line but never saved it to ~/.bashrc. Add it there.

alias does not work in a script / under sudo

That is expected: aliases are not inherited by a non-interactive shell or by sudo. Solutions: a function in ~/.bashrc, or sudo with the full command. To reuse it as root: sudo bash -ic 'alias_name' (crude), or just spell out the command by hand.

I need to temporarily run the real command

The \ prefix (\rm file) or command rm file runs the original, bypassing the alias.

See also

  • env-vars — env, export, $PATH, ~/.bashrc vs profiles; why "command not found".
  • which — Find which binary would run, whether it's a builtin or an alias, where it lives and its man page.
  • bash-scripting — Variables, conditionals, loops, arguments, and the safe mode set -euo pipefail.
Loading…